Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
661 changes: 0 additions & 661 deletions src/Simulation/QsharpCore/Intrinsic.qs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<Import Project="..\Common\AssemblyCommon.props" />
<Import Project="..\Common\DebugSymbols.props" />

<Import Project="..\TargetDefinitions\TargetPackages\QsharpCore.Package.props" />

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<QsharpDocsGeneration>true</QsharpDocsGeneration>
Expand Down
127 changes: 0 additions & 127 deletions src/Simulation/QsharpCore/Reset.qs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// Licensed under the MIT License.

namespace Microsoft.Quantum.Arrays {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;

/// # Summary
/// Given an array, returns a range over the indices of that array, suitable
/// for use in a for loop.
Expand All @@ -29,5 +26,4 @@ namespace Microsoft.Quantum.Arrays {
function IndexRange<'TElement>(array : 'TElement[]) : Range {
return 0..(Length(array) - 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.Quantum.Diagnostics {
/// <xref:microsoft.quantum.diagnostics.assertqubitisinstatewithintolerance> allows for asserting
/// arbitrary qubit states rather than only $Z$ eigenstates.
operation AssertQubit (expected : Result, q : Qubit) : Unit {
Assert([PauliZ], [q], expected, $"Qubit in invalid state. Expecting: {expected}");
AssertMeasurement([PauliZ], [q], expected, $"Qubit in invalid state. Expecting: {expected}");
}

/// # Summary
Expand All @@ -47,7 +47,7 @@ namespace Microsoft.Quantum.Diagnostics {
/// <xref:microsoft.quantum.diagnostics.assertqubitisinstatewithintolerance> allows for asserting
/// arbitrary qubit states rather than only $Z$ eigenstates.
operation AssertQubitWithinTolerance(expected : Result, q : Qubit, tolerance : Double) : Unit {
AssertProb([PauliZ], [q], expected, 1.0, $"Qubit in invalid state. Expecting: {expected} with tolerance {tolerance}", tolerance);
AssertMeasurementProbability([PauliZ], [q], expected, 1.0, $"Qubit in invalid state. Expecting: {expected} with tolerance {tolerance}", tolerance);
}

/// # Summary
Expand Down Expand Up @@ -126,10 +126,10 @@ namespace Microsoft.Quantum.Diagnostics {
// Probability of getting outcome One in measuring PauliZ is Tr(M(I-Z)/2) = (mi-mz)/2.0
// similarly, we find the probabilities for measuring PauliX,PauliY
let tol = tolerance / 2.0;
AssertProb([PauliX], [register], Zero, (mi + mx) / 2.0, $"Qubit Zero probability on X basis failed", tol);
AssertProb([PauliY], [register], Zero, (mi + my) / 2.0, $"Qubit Zero probability on Y basis failed", tol);
AssertProb([PauliZ], [register], Zero, (mi + mz) / 2.0, $"Qubit Zero probability on Z basis failed", tol);
AssertProb([PauliZ], [register], One, (mi - mz) / 2.0, $"Qubit One probability on Z basis failed", tol);
AssertMeasurementProbability([PauliX], [register], Zero, (mi + mx) / 2.0, $"Qubit Zero probability on X basis failed", tol);
AssertMeasurementProbability([PauliY], [register], Zero, (mi + my) / 2.0, $"Qubit Zero probability on Y basis failed", tol);
AssertMeasurementProbability([PauliZ], [register], Zero, (mi + mz) / 2.0, $"Qubit Zero probability on Z basis failed", tol);
AssertMeasurementProbability([PauliZ], [register], One, (mi - mz) / 2.0, $"Qubit One probability on Z basis failed", tol);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,41 @@ namespace Microsoft.Quantum.Simulation.TestSuite {
open Microsoft.Quantum.Simulation.TestSuite.Math;


internal operation FlipToBasis (basis : Int[], qubits : Qubit[]) : Unit is Adj + Ctl {
if (Length(qubits) != Length(basis))
{
fail "qubits and stateIds must have the same length";
}

for (i in 0 .. Length(qubits) - 1)
{
let id = basis[i];
let qubit = qubits[i];

if (id < 0 or id > 3) {
fail $"Invalid basis. Must be between 0 and 3, it was {basis}";
}

if (id == 0)
{
I(qubit);
}
elif (id == 1)
{
X(qubit);
}
elif (id == 2)
{
H(qubit);
}
else
{
H(qubit);
S(qubit);
}
}
}

operation AssertProbMultiQubitTest () : Unit {


Expand Down Expand Up @@ -51,15 +86,14 @@ namespace Microsoft.Quantum.Simulation.TestSuite {
}

using (qubits = Qubit[l]) {
_flipToBasis(stateId, qubits);
FlipToBasis(stateId, qubits);
let expectedZeroProbability = 0.5 + 0.5 * ExpectedValueForMultiPauliByStateId(observable, stateId);
let expectedOneProbability = 1.0 - expectedZeroProbability;
AssertProb(observable, qubits, Zero, expectedZeroProbability, $"", Accuracy());
AssertProb(observable, qubits, One, expectedOneProbability, $"", Accuracy());
Adjoint _flipToBasis(stateId, qubits);
AssertMeasurementProbability(observable, qubits, Zero, expectedZeroProbability, $"", Accuracy());
AssertMeasurementProbability(observable, qubits, One, expectedOneProbability, $"", Accuracy());
Adjoint FlipToBasis(stateId, qubits);
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.Quantum.Simulation.TestSuite {

for (stateId in 0 .. maxId) {
let expectedState = ApplyMatrix(unitaryMatrix, StateIdToVector(stateId));
_flipToBasis([stateId], [qubit]);
FlipToBasis([stateId], [qubit]);
unitaryOp(qubit);
let alpha = Microsoft.Quantum.Math.Complex((expectedState![0])!);
let beta = Microsoft.Quantum.Math.Complex((expectedState![1])!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Quantum.Simulation.TestSuite {
operation AssertUnitaryHelper (stateIds : Int[], unitaryMatrix : RowMajorMatrix, unitaryOp : (Qubit[] => Unit), qubits : Qubit[]) : Unit {

let expectedState = ApplyMatrix(unitaryMatrix, StateById(stateIds));
_flipToBasis(stateIds, qubits);
FlipToBasis(stateIds, qubits);
unitaryOp(qubits);
AssertState(expectedState, qubits);
ResetAll(qubits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Microsoft.Quantum.Simulation.TestSuite {
}

mutable states = new Vector[numQubits];
_flipToBasis(inputStateId, qubits);
FlipToBasis(inputStateId, qubits);

for (i in 0 .. numQubits - 1) {
let (op, matrix) = operationsToTest[i]!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Quantum.Diagnostics {
/// Iterates a variable through a Cartesian product
/// [ 0, bounds[0]-1 ] × [ 0, bounds[1]-1 ] × [ 0, bounds[Length(bounds)-1]-1 ]
/// and calls op(arr) for every element of the Cartesian product
operation _iterateThroughCartesianPower (length : Int, value : Int, op : (Int[] => Unit)) : Unit {
internal operation IterateThroughCartesianPower (length : Int, value : Int, op : (Int[] => Unit)) : Unit {
mutable bounds = new Int[length];

for (i in 0 .. length - 1)
Expand Down Expand Up @@ -67,12 +67,10 @@ namespace Microsoft.Quantum.Diagnostics {
/// ## basis
/// Array of single-qubit basis state IDs (0 <= id <= 3), one for each element of
/// qubits.
operation _flipToBasis (basis : Int[], qubits : Qubit[]) : Unit
is Adj + Ctl {

internal operation FlipToBasis (basis : Int[], qubits : Qubit[]) : Unit is Adj + Ctl {
if (Length(qubits) != Length(basis))
{
fail $"qubits and stateIds must have the same length";
fail "qubits and stateIds must have the same length";
}

for (i in 0 .. Length(qubits) - 1)
Expand Down Expand Up @@ -108,7 +106,7 @@ namespace Microsoft.Quantum.Diagnostics {
/// # Summary
/// Checks if the result of applying two operations `givenU` and `expectedU` to
/// a basis state is the same. The basis state is described by `basis` parameter.
/// See <xref:microsoft.quantum.extensions.fliptobasis> function for more details on this
/// See <xref:microsoft.quantum.extensions.FlipToBasis> function for more details on this
/// description.
///
/// # Input
Expand All @@ -119,15 +117,15 @@ namespace Microsoft.Quantum.Diagnostics {
/// Operation on $n$ qubits to be checked.
/// ## expectedU
/// Reference operation on $n$ qubits that givenU is to be compared against.
operation _assertEqualOnBasisVector (basis : Int[], givenU : (Qubit[] => Unit), expectedU : (Qubit[] => Unit is Adj)) : Unit {
internal operation AssertEqualOnBasisVector (basis : Int[], givenU : (Qubit[] => Unit), expectedU : (Qubit[] => Unit is Adj)) : Unit {
let tolerance = 1e-5;

using (qubits = Qubit[Length(basis)]) {
AssertAllZeroWithinTolerance(qubits, tolerance);
_flipToBasis(basis, qubits);
FlipToBasis(basis, qubits);
givenU(qubits);
Adjoint expectedU(qubits);
Adjoint _flipToBasis(basis, qubits);
Adjoint FlipToBasis(basis, qubits);
AssertAllZeroWithinTolerance(qubits, tolerance);
}
}
Expand Down Expand Up @@ -157,8 +155,8 @@ namespace Microsoft.Quantum.Diagnostics {
/// described in [ *I. L. Chuang, M. A. Nielsen* ](https://arxiv.org/abs/quant-ph/9610001).
operation AssertOperationsEqualInPlace(nQubits : Int, givenU : (Qubit[] => Unit), expectedU : (Qubit[] => Unit is Adj)) : Unit
{
let checkOperation = _assertEqualOnBasisVector(_, givenU, expectedU);
_iterateThroughCartesianPower(nQubits, 4, checkOperation);
let checkOperation = AssertEqualOnBasisVector(_, givenU, expectedU);
IterateThroughCartesianPower(nQubits, 4, checkOperation);
}

/// # Summary
Expand All @@ -177,8 +175,8 @@ namespace Microsoft.Quantum.Diagnostics {
/// Reference operation on $n$ qubits that `givenU` is to be compared against.
operation AssertOperationsEqualInPlaceCompBasis (nQubits : Int, givenU : (Qubit[] => Unit), expectedU : (Qubit[] => Unit is Adj)) : Unit
{
let checkOperation = _assertEqualOnBasisVector(_, givenU, expectedU);
_iterateThroughCartesianPower(nQubits, 2, checkOperation);
let checkOperation = AssertEqualOnBasisVector(_, givenU, expectedU);
IterateThroughCartesianPower(nQubits, 2, checkOperation);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Quantum.Diagnostics {
/// A qubit array in the $\ket{0\cdots 0}$ state
/// ## right
/// A qubit array in the $\ket{0\cdots 0}$ state
operation _prepareEntangledState (left : Qubit[], right : Qubit[]) : Unit
internal operation PrepareEntangledState (left : Qubit[], right : Qubit[]) : Unit
is Adj + Ctl {

for (idxQubit in 0 .. Length(left) - 1)
Expand Down Expand Up @@ -56,10 +56,10 @@ namespace Microsoft.Quantum.Diagnostics {
operation AssertOperationsEqualReferenced (nQubits : Int, actual : (Qubit[] => Unit), expected : (Qubit[] => Unit is Adj)) : Unit {
// Prepare a reference register entangled with the target register.
using ((reference, target) = (Qubit[nQubits], Qubit[nQubits])) {
_prepareEntangledState(reference, target);
PrepareEntangledState(reference, target);
actual(target);
Adjoint expected(target);
Adjoint _prepareEntangledState(reference, target);
Adjoint PrepareEntangledState(reference, target);
AssertAllZero(reference + target);
ResetAll(target);
ResetAll(reference);
Expand Down
27 changes: 27 additions & 0 deletions src/Simulation/TargetDefinitions/Decompositions/CCNOT.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Quantum.Intrinsic {
/// # Summary
/// Applies the doubly controlled–NOT (CCNOT) gate to three qubits.
///
/// # Input
/// ## control1
/// First control qubit for the CCNOT gate.
/// ## control2
/// Second control qubit for the CCNOT gate.
/// ## target
/// Target qubit for the CCNOT gate.
///
/// # Remarks
/// Equivalent to:
/// ```qsharp
/// Controlled X([control1, control2], target);
/// ```
operation CCNOT (control1 : Qubit, control2 : Qubit, target : Qubit) : Unit is Adj + Ctl {
body (...) {
Controlled X([control1, control2], target);
}
adjoint self;
}
}
Loading