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
216 changes: 103 additions & 113 deletions src/Simulation/Simulators/SparseSimulator/SparseSimQSharpTests/Program.qs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,37 @@ namespace Microsoft.Quantum.SparseSimulatorTests {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Diagnostics;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Preparation;
open Microsoft.Quantum.Math;

operation DumpIdRotation() : Unit {
use qubits = Qubit[2] {
H(qubits[0]);
H(qubits[1]);
(Controlled Exp)([qubits[0]], ([PauliI], 0.5, [qubits[1]]));
DumpMachine();
ResetAll(qubits);
internal operation ApplyToEachCA<'T> (singleElementOperation : ('T => Unit is Adj + Ctl), register : 'T[])
: Unit is Adj + Ctl {
for idxQubit in IndexRange(register) {
singleElementOperation(register[idxQubit]);
}
}

internal operation ApplyToFirstTwoQubitsCA (op : ((Qubit, Qubit) => Unit is Adj + Ctl), register : Qubit[])
: Unit is Adj + Ctl {
if (Length(register) < 2)
{
fail $"Must have at least two qubits to act on.";
}

op(register[0], register[1]);
}

internal function Zipped<'T, 'U>(left : 'T[], right : 'U[]) : ('T, 'U)[] {
let nElements = Length(left) < Length(right)
? Length(left)
| Length(right);
mutable output = new ('T, 'U)[nElements];

for idxElement in 0 .. nElements - 1 {
set output w/= idxElement <- (left[idxElement], right[idxElement]);
}

return output;
}

operation _R(pauli : Pauli, theta : Double, qubits : Qubit[]) : Unit is Adj + Ctl {
Expand Down Expand Up @@ -50,24 +68,12 @@ namespace Microsoft.Quantum.SparseSimulatorTests {
}
}

operation DumpMultiplexZ() : Unit {
use qubits = Qubit[12] {
ApplyToEach(H, qubits[0..5]);
ApplyToEach(CNOT, Zipped(qubits[0..5], qubits[6..11]));
ApproximatelyMultiplexZ(0.001, [0.12, 0.34, -0.26, 0.5, 1.8], LittleEndian(qubits[6..10]), qubits[11]);
DumpMachine();
ResetAll(qubits);
}
}



internal operation ControlledRz(angle : Double, (control : Qubit, target : Qubit)) : Unit is Adj {
internal operation ControlledRz(angle : Double, (control : Qubit, target : Qubit)) : Unit is Adj + Ctl {
Controlled Rz([control], (angle, target));
DumpMachine();
}

internal operation ControlledRzAsR1(angle : Double, (control : Qubit, target : Qubit)) : Unit is Adj {
internal operation ControlledRzAsR1(angle : Double, (control : Qubit, target : Qubit)) : Unit is Adj + Ctl {
Controlled R1([control], (angle, target));
R1(-angle / 2.0, control);
DumpMachine();
Expand All @@ -76,7 +82,7 @@ namespace Microsoft.Quantum.SparseSimulatorTests {
operation TestEqualityOfControlledRz() : Unit {
for _ in 1..10 {
let angle = Microsoft.Quantum.Random.DrawRandomDouble(0.0, 2.0 * PI());
AssertOperationsEqualReferenced(2, ApplyToFirstTwoQubits(ControlledRzAsR1(angle, _), _), ApplyToFirstTwoQubitsA(ControlledRz(angle, _), _));
AssertOperationsEqualReferenced(2, ApplyToFirstTwoQubitsCA(ControlledRzAsR1(angle, _), _), ApplyToFirstTwoQubitsCA(ControlledRz(angle, _), _));
}
}

Expand All @@ -90,8 +96,8 @@ namespace Microsoft.Quantum.SparseSimulatorTests {
LargeStateTestWrapper(CNOTTest, nqubits);
LargeStateTestWrapper(ResetTest, nqubits);
LargeStateTestWrapper(AssertTest, nqubits);
LargeStateTestWrapper(AndChainTest, nqubits);
LargeStateTestWrapper(CZTest, nqubits);
//LargeStateTestWrapper(AndChainTest, nqubits);
//LargeStateTestWrapper(CZTest, nqubits);
LargeStateTestWrapper(AllocationTest, nqubits);
LargeStateTestWrapper(Rotation1CompareTest, nqubits);
LargeStateTestWrapper(RotationFracCompareTest, nqubits);
Expand Down Expand Up @@ -128,7 +134,7 @@ namespace Microsoft.Quantum.SparseSimulatorTests {
@Test("Microsoft.Quantum.SparseSimulation.SparseSimulator")
operation PartialDumpTest() : Unit {
use qubits = Qubit[4] {
ApplyToEach(H, qubits);
ApplyToEachCA(H, qubits);
CNOT(qubits[2], qubits[3]);
DumpRegister("Test_file_1", qubits[0..1]);
DumpRegister("Test_file_2", qubits[2..3]);
Expand Down Expand Up @@ -219,96 +225,80 @@ namespace Microsoft.Quantum.SparseSimulatorTests {
}

// Taken from the PurifiedMixedState documentation
@Test("Microsoft.Quantum.SparseSimulation.SparseSimulator")
operation QROMPrepareTest() : Unit {
let coefficients = [1.0, 2.0, 3.0, 4.0, 5.0];
let targetError = 1e-3;
let purifiedState = PurifiedMixedState(targetError, coefficients);
use indexRegister = Qubit[purifiedState::Requirements::NIndexQubits] {
use garbageRegister = Qubit[purifiedState::Requirements::NGarbageQubits] {
purifiedState::Prepare(LittleEndian(indexRegister), new Qubit[0], garbageRegister);
ResetAll(garbageRegister);
}
ResetAll(indexRegister);
}
}
//@Test("Microsoft.Quantum.SparseSimulation.SparseSimulator")
//operation QROMPrepareTest() : Unit {
// let coefficients = [1.0, 2.0, 3.0, 4.0, 5.0];
// let targetError = 1e-3;
// let purifiedState = PurifiedMixedState(targetError, coefficients);
// use indexRegister = Qubit[purifiedState::Requirements::NIndexQubits] {
// use garbageRegister = Qubit[purifiedState::Requirements::NGarbageQubits] {
// purifiedState::Prepare(LittleEndian(indexRegister), new Qubit[0], garbageRegister);
// ResetAll(garbageRegister);
// }
// ResetAll(indexRegister);
// }
//}


@Test("Microsoft.Quantum.SparseSimulation.SparseSimulator")
operation CZTest() : Unit {
let num_qubits = 5;
use qubits = Qubit[num_qubits]{
H(qubits[0]);
for idx in 0..(2^(num_qubits-1) - 1) {
let result = (idx == 2^(num_qubits - 1) - 1);
within {
ApplyXorInPlace(idx, LittleEndian(qubits[1..num_qubits - 1]));
(Controlled Z)(qubits[1..num_qubits -1], (qubits[0]));
} apply {
if (result){
AssertMeasurement([PauliX], qubits[0..0], One, "CZ failed to add phase");
} else {
AssertMeasurement([PauliX], qubits[0..0], Zero, "CZ added unexpected phase");
}
}
}
H(qubits[0]);
}
}
//@Test("Microsoft.Quantum.SparseSimulation.SparseSimulator")
//operation CZTest() : Unit {
// let num_qubits = 5;
// use qubits = Qubit[num_qubits]{
// H(qubits[0]);
// for idx in 0..(2^(num_qubits-1) - 1) {
// let result = (idx == 2^(num_qubits - 1) - 1);
// within {
// ApplyXorInPlace(idx, LittleEndian(qubits[1..num_qubits - 1]));
// (Controlled Z)(qubits[1..num_qubits -1], (qubits[0]));
// } apply {
// if (result){
// AssertMeasurement([PauliX], qubits[0..0], One, "CZ failed to add phase");
// } else {
// AssertMeasurement([PauliX], qubits[0..0], Zero, "CZ added unexpected phase");
// }
// }
// }
// H(qubits[0]);
// }
//}

operation ApplyAndChain(andOp : ((Qubit, Qubit, Qubit)=>Unit is Adj + Ctl), auxRegister : Qubit[], ctrlRegister : Qubit[], target : Qubit)
: Unit is Adj {
if (Length(ctrlRegister) == 0) {
X(target);
} elif (Length(ctrlRegister) == 1) {
CNOT(Head(ctrlRegister), target);
} else {
EqualityFactI(Length(auxRegister), Length(ctrlRegister) - 2, "Unexpected number of auxiliary qubits");
let controls1 = ctrlRegister[0..0] + auxRegister;
let controls2 = Rest(ctrlRegister);
let targets = auxRegister + [target];
ApplyToEachA(andOp, Zipped3(controls1, controls2, targets));
}
}
//operation ApplyAndChain(andOp : ((Qubit, Qubit, Qubit)=>Unit is Adj + Ctl), auxRegister : Qubit[], ctrlRegister : /Qubit/[], target : Qubit)
//: Unit is Adj {
// if (Length(ctrlRegister) == 0) {
// X(target);
// } elif (Length(ctrlRegister) == 1) {
// CNOT(ctrlRegister[0], target);
// } else {
// EqualityFactI(Length(auxRegister), Length(ctrlRegister) - 2, "Unexpected number of auxiliary qubits");
// let controls1 = ctrlRegister[0..0] + auxRegister;
// let controls2 = ctrlRegister[1...];
// let targets = auxRegister + [target];
// ApplyToEachCA(andOp, Zipped3(controls1, controls2, targets));
// }
//}

operation AndChainDump() : Unit {
let num_qubits = 5;
use qubits = Qubit[num_qubits]{
use aux = Qubit[num_qubits - 3]{
within{
for idx in 1..num_qubits - 1 {
H(qubits[idx]);
if (idx % 3 == 0){ Z(qubits[idx]);}
}
ApplyAndChain(ApplyAnd, aux, qubits[1..num_qubits -1], qubits[0]);
} apply {
DumpMachine();
}
}
}
}
@Test("Microsoft.Quantum.SparseSimulation.SparseSimulator")
operation AndChainTest() : Unit {
let num_qubits = 5;
use qubits = Qubit[num_qubits]{
use aux = Qubit[num_qubits - 3]{
for idx in 0..(2^(num_qubits-1) - 1) {
let result = (idx == 2^(num_qubits - 1) - 1);
within {
ApplyXorInPlace(idx, LittleEndian(qubits[1..num_qubits - 1]));
ApplyAndChain(ApplyAnd, aux, qubits[1..num_qubits -1], qubits[0]);
} apply {
let after = M(qubits[0]);
if (result){
Fact(after == One, "Did not apply AND");
} else {
Fact(after == Zero, "Applied AND unexpectedly");
}
}
}
}
}
}
//@Test("Microsoft.Quantum.SparseSimulation.SparseSimulator")
//operation AndChainTest() : Unit {
// let num_qubits = 5;
// use qubits = Qubit[num_qubits]{
// use aux = Qubit[num_qubits - 3]{
// for idx in 0..(2^(num_qubits-1) - 1) {
// let result = (idx == 2^(num_qubits - 1) - 1);
// within {
// ApplyXorInPlace(idx, LittleEndian(qubits[1..num_qubits - 1]));
// ApplyAndChain(ApplyAnd, aux, qubits[1..num_qubits -1], qubits[0]);
// } apply {
// let after = M(qubits[0]);
// if (result){
// Fact(after == One, "Did not apply AND");
// } else {
// Fact(after == Zero, "Applied AND unexpectedly");
// }
// }
// }
// }
// }
//}


operation DumpMCXFrac() : Unit {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.15.2101126940">
<Project Sdk="Microsoft.Quantum.Sdk">

<Import Project="..\..\..\Common\Simulators.Test.props" />

Expand Down