This commit is contained in:
Christopher Granade 2021-05-10 09:54:34 -07:00
Родитель dae271baad
Коммит ed4d272d7c
7 изменённых файлов: 462 добавлений и 534 удалений

Просмотреть файл

@ -15,7 +15,7 @@ namespace Microsoft.Quantum.Research.Samples {
/// Applies the time evolution unitary generated by Hamiltonian /// Applies the time evolution unitary generated by Hamiltonian
/// H = X_1 + Z_1 Z_2, as simulated using a simple Trotter decomposition. /// H = X_1 + Z_1 Z_2, as simulated using a simple Trotter decomposition.
operation EvolveByTrotter (time: Double, nStep: Int, qubits: Qubit[]) : Unit is Adj + Ctl { operation EvolveByTrotter (time: Double, nStep: Int, qubits: Qubit[]) : Unit is Adj + Ctl {
for (idxStep in 1..nStep) { for idxStep in 1..nStep {
Exp([PauliZ, PauliZ], -time / IntAsDouble(nStep), qubits); Exp([PauliZ, PauliZ], -time / IntAsDouble(nStep), qubits);
Exp([PauliX, PauliI], -time / IntAsDouble(nStep), qubits); Exp([PauliX, PauliI], -time / IntAsDouble(nStep), qubits);
} }
@ -35,7 +35,11 @@ namespace Microsoft.Quantum.Research.Samples {
let idxPhysical2 = 2; let idxPhysical2 = 2;
ExpFrac([PauliI], -1, 1, [qubits[idxControl]]); // global phase -i. ExpFrac([PauliI], -1, 1, [qubits[idxControl]]); // global phase -i.
ApplyWithCA(X, CX(_, qubits[idxPhysical1]), qubits[idxControl]); // control-X activated on |0> rather than |1>. within { // control-X activated on |0> rather than |1>.
X(qubits[idxControl]);
} apply {
CX(qubits[idxControl], qubits[idxPhysical1]);
}
ApplyToEachCA(CZ(qubits[idxControl], _), qubits[idxPhysical1..idxPhysical2]); ApplyToEachCA(CZ(qubits[idxControl], _), qubits[idxPhysical1..idxPhysical2]);
X(qubits[idxControl]); // This is 2|G><G|-1, where |G> happens to be (|0> + |1>)/sqrt(2). X(qubits[idxControl]); // This is 2|G><G|-1, where |G> happens to be (|0> + |1>)/sqrt(2).
} }
@ -69,29 +73,28 @@ namespace Microsoft.Quantum.Research.Samples {
fail "An odd number of calls to the quantum walk operator is forbidden."; fail "An odd number of calls to the quantum walk operator is forbidden.";
} }
using (aux = Qubit()) { use aux = Qubit();
H(aux); // Preparing the auxillary qubit in the |+⟩ state. H(aux); // Preparing the auxillary qubit in the |+⟩ state.
for (idx in 0..((nQueries / 2) - 1)) { for idx in 0..((nQueries / 2) - 1) {
within { within {
Rz(-angles[2 * idx], aux); Rz(-angles[2 * idx], aux);
H(aux); H(aux);
X(aux); X(aux);
} apply { } apply {
(Controlled walkOperation)([aux], qubits); (Controlled walkOperation)([aux], qubits);
}
within {
Rz(-angles[2 * idx + 1], aux);
H(aux);
} apply {
Controlled Adjoint walkOperation([aux], qubits);
}
} }
Rz(angles[nQueries], aux); // The last unitary correction.
if (MResetX(aux) == One) { within {
fail "QSP's postselection failed."; Rz(-angles[2 * idx + 1], aux);
H(aux);
} apply {
Controlled Adjoint walkOperation([aux], qubits);
} }
}
Rz(angles[nQueries], aux); // The last unitary correction.
if (MResetX(aux) == One) {
fail "QSP's postselection failed.";
} }
} }
@ -108,15 +111,14 @@ namespace Microsoft.Quantum.Research.Samples {
/// which should be called beforehand by a C# driver. /// which should be called beforehand by a C# driver.
operation SampleHamiltonianEvolutionByQSP(tau : Double, angles : Double[]) operation SampleHamiltonianEvolutionByQSP(tau : Double, angles : Double[])
: Unit { : Unit {
using (qubits = Qubit[3]) { use qubits = Qubit[3];
H(Head(qubits)); // Preparing an auxillary qubit in the state $\ket{G}$. H(Head(qubits)); // Preparing an auxillary qubit in the state $\ket{G}$.
DumpMachine(()); DumpMachine(());
QuantumSignalProcessOnEquator(angles, ApplyBlockEncodedHamiltonian, qubits); QuantumSignalProcessOnEquator(angles, ApplyBlockEncodedHamiltonian, qubits);
// Now we roll back by Trotter evolution. // Now we roll back by Trotter evolution.
EvolveByTrotter(-tau / 2.0, 1000, qubits[1..2]); EvolveByTrotter(-tau / 2.0, 1000, qubits[1..2]);
// The factor of 2 that divides tau is the 1-norm of the Hamiltonian's coefficients. // The factor of 2 that divides tau is the 1-norm of the Hamiltonian's coefficients.
DumpMachine(()); DumpMachine(());
ResetAll(qubits); ResetAll(qubits);
}
} }
} }

Просмотреть файл

@ -10,20 +10,18 @@ namespace Microsoft.Quantum.Research.Characterization {
open Microsoft.Quantum.Oracles; open Microsoft.Quantum.Oracles;
open Microsoft.Quantum.Characterization; open Microsoft.Quantum.Characterization;
operation _PrepAndMeasurePhaseEst( internal operation _PrepAndMeasurePhaseEst(
wInv : Double, wInv : Double,
t : Double, t : Double,
op : ((Double, Double, Qubit) => Unit)) op : ((Double, Double, Qubit) => Unit))
: Result { : Result {
using (target = Qubit()) { use target = Qubit();
op(t, wInv, target); op(t, wInv, target);
return MResetZ(target); return MResetZ(target);
}
} }
// NB: we take std.dev instead of variance here to avoid having to take a square root. // NB: we take std.dev instead of variance here to avoid having to take a square root.
/// # Summary /// # Summary
/// Performs iterative phase estimation using a random walk to approximate /// Performs iterative phase estimation using a random walk to approximate
/// Bayesian inference on the classical measurement results from a given /// Bayesian inference on the classical measurement results from a given
@ -109,79 +107,39 @@ namespace Microsoft.Quantum.Research.Characterization {
mutable datum = Zero; mutable datum = Zero;
mutable nTotalMeasurements = 0; mutable nTotalMeasurements = 0;
mutable nAcceptedMeasurements = 0; mutable nAcceptedMeasurements = 0;
repeat { repeat {
if (nTotalMeasurements >= maxMeasurements) { if nTotalMeasurements >= maxMeasurements {
return mu; return mu;
} }
let wInv = mu - (PI() * sigma) / 2.0; let wInv = mu - (PI() * sigma) / 2.0;
let t = 1.0 / sigma; let t = 1.0 / sigma;
set datum = sampleOp(wInv, t); set datum = sampleOp(wInv, t);
set nTotalMeasurements += 1; set nTotalMeasurements += 1;
if (datum == Zero) { if datum == Zero {
set mu -= sigma * INV_SQRT_E; set mu -= sigma * INV_SQRT_E;
} else { } else {
set mu += sigma * INV_SQRT_E; set mu += sigma * INV_SQRT_E;
} }
set sigma *= PREFACTOR; set sigma *= PREFACTOR;
set dataRecord += [datum]; set dataRecord += [datum];
// Perform consistency check. // Perform consistency check.
if (nTotalMeasurements >= maxMeasurements) { if (nTotalMeasurements >= maxMeasurements) {
return mu; return mu;
} }
if (unwind > 0) {
mutable checkDatum = sampleOp(mu, 1.0 / sigma);
set nTotalMeasurements = nTotalMeasurements + 1;
if (checkDatum == One) {
repeat {
for (idxUnwind in 0 .. unwind - 1) {
set sigma /= PREFACTOR;
if (Length(dataRecord) > 0) {
let unwoundDatum = Tail(dataRecord);
set dataRecord = Most(dataRecord);
if (unwoundDatum == Zero) {
set mu += sigma * INV_SQRT_E;
}
else {
set mu -= sigma * INV_SQRT_E;
}
}
}
if (nTotalMeasurements >= maxMeasurements) {
Message("RWPE used too many measurements during unwinding.");
return mu;
}
set checkDatum = sampleOp(mu, 1.0 / sigma);
set nTotalMeasurements += 1;
}
until (checkDatum == Zero)
fixup {
}
}
}
set nAcceptedMeasurements = nAcceptedMeasurements + 1; set nAcceptedMeasurements = nAcceptedMeasurements + 1;
} }
until (nAcceptedMeasurements >= nMeasurements) until nAcceptedMeasurements >= nMeasurements;
fixup {
}
return mu; return mu;
} }
} }

Просмотреть файл

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2003.3107"> <Project Sdk="Microsoft.Quantum.Sdk/0.16.2104138035">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>

Просмотреть файл

@ -48,8 +48,8 @@ namespace Microsoft.Quantum.Research.Chemistry {
// Circuit with cancellation of neighbouring CNOTS // Circuit with cancellation of neighbouring CNOTS
let (minInt, bitStringApplyCNOT) = _DeltaParityCNOTbitstring(prevFermionicTerm, nextFermionicTerm); let (minInt, bitStringApplyCNOT) = _DeltaParityCNOTbitstring(prevFermionicTerm, nextFermionicTerm);
for (idx in 0 .. Length(bitStringApplyCNOT) - 1) { for idx in 0 .. Length(bitStringApplyCNOT) - 1 {
if (bitStringApplyCNOT[idx] == true) { if bitStringApplyCNOT[idx] == true {
CNOT(qubits[idx + minInt], aux); CNOT(qubits[idx + minInt], aux);
} }
} }
@ -74,21 +74,19 @@ namespace Microsoft.Quantum.Research.Chemistry {
mutable prevBitString = new Bool[nInts]; mutable prevBitString = new Bool[nInts];
mutable nextBitString = new Bool[nInts]; mutable nextBitString = new Bool[nInts];
for (idxGroup in 0 .. Length(prevFermionicTerm) / 2 - 1) { for idxGroup in 0 .. Length(prevFermionicTerm) / 2 - 1 {
for idxQubit in (prevFermionicTerm[idxGroup * 2] + 1) - minInt .. (prevFermionicTerm[idxGroup * 2 + 1] - 1) - minInt {
for (idxQubit in (prevFermionicTerm[idxGroup * 2] + 1) - minInt .. (prevFermionicTerm[idxGroup * 2 + 1] - 1) - minInt) {
set prevBitString w/= idxQubit <- true; set prevBitString w/= idxQubit <- true;
} }
} }
for (idxGroup in 0 .. Length(nextFermionicTerm) / 2 - 1) { for idxGroup in 0 .. Length(nextFermionicTerm) / 2 - 1 {
for idxQubit in (nextFermionicTerm[idxGroup * 2] + 1) - minInt .. (nextFermionicTerm[idxGroup * 2 + 1] - 1) - minInt {
for (idxQubit in (nextFermionicTerm[idxGroup * 2] + 1) - minInt .. (nextFermionicTerm[idxGroup * 2 + 1] - 1) - minInt) {
set nextBitString w/= idxQubit <- true; set nextBitString w/= idxQubit <- true;
} }
} }
for (idx in 0 .. nInts - 1) { for idx in 0 .. nInts - 1 {
set nextBitString w/= idx <- Xor(prevBitString[idx], nextBitString[idx]); set nextBitString w/= idx <- Xor(prevBitString[idx], nextBitString[idx]);
} }
@ -127,7 +125,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
targetQubit : Qubit targetQubit : Qubit
) )
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
for ((op, target) in Zip(ops, qubits + [targetQubit])) { for (op, target) in Zipped(ops, qubits + [targetQubit]) {
op(target); op(target);
} }
} }
@ -142,9 +140,9 @@ namespace Microsoft.Quantum.Research.Chemistry {
/// Angle of Rz rotation. /// Angle of Rz rotation.
/// ## parityQubit /// ## parityQubit
/// Qubit that determines the sign of time-evolution. /// Qubit that determines the sign of time-evolution.
/// ## qubits /// ## qubit
/// Qubit acted on by Rz. /// Qubit acted on by Rz.
operation _JWOptimizedZ(angle : Double, parityQubit : Qubit, qubit : Qubit) internal operation _JWOptimizedZ(angle : Double, parityQubit : Qubit, qubit : Qubit)
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
ApplyWithCA(CNOT(parityQubit, _), Rz(-2.0 * angle, _), qubit); ApplyWithCA(CNOT(parityQubit, _), Rz(-2.0 * angle, _), qubit);
} }
@ -162,7 +160,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
/// Qubit that determines the sign of time-evolution. /// Qubit that determines the sign of time-evolution.
/// ## qubits /// ## qubits
/// Qubits of Hamiltonian. /// Qubits of Hamiltonian.
operation _JWOptimizedZTerm(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[]) internal operation _JWOptimizedZTerm(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[])
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
let ((idxTermType, coeff), idxFermions) = term!; let ((idxTermType, coeff), idxFermions) = term!;
let angle = (1.0 * coeff[0]) * stepSize; let angle = (1.0 * coeff[0]) * stepSize;
@ -183,7 +181,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
/// Qubit that determines the sign of time-evolution. /// Qubit that determines the sign of time-evolution.
/// ## qubits /// ## qubits
/// Qubits of Hamiltonian. /// Qubits of Hamiltonian.
operation _JWOptimizedZZTerm(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[]) internal operation _JWOptimizedZZTerm(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[])
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
let ((idxTermType, coeff), idxFermions) = term!; let ((idxTermType, coeff), idxFermions) = term!;
let angle = (1.0 * coeff[0]) * stepSize; let angle = (1.0 * coeff[0]) * stepSize;
@ -204,7 +202,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
/// Qubit that determines the sign of time-evolution. /// Qubit that determines the sign of time-evolution.
/// ## qubits /// ## qubits
/// Qubits of Hamiltonian. /// Qubits of Hamiltonian.
operation _JWOptimizedHpqTerm(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[]) internal operation _JWOptimizedHpqTerm(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[])
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
let ((idxTermType, coeff), idxFermions) = term!; let ((idxTermType, coeff), idxFermions) = term!;
ApplyWithCA(ApplyDeltaParity(new Int[0], idxFermions, parityQubit, _), _JWOptimizedHpqTermImpl(term, stepSize, parityQubit, _), qubits); ApplyWithCA(ApplyDeltaParity(new Int[0], idxFermions, parityQubit, _), _JWOptimizedHpqTermImpl(term, stepSize, parityQubit, _), qubits);
@ -213,7 +211,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
/// # Summary /// # Summary
/// Implementation step of `JWOptimizedHpqTerm_`. /// Implementation step of `JWOptimizedHpqTerm_`.
operation _JWOptimizedHpqTermImpl ( internal operation _JWOptimizedHpqTermImpl (
term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[] term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[]
) )
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
@ -224,7 +222,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
let qubitsPQ = Subarray(idxFermions[0 .. 1], qubits); let qubitsPQ = Subarray(idxFermions[0 .. 1], qubits);
let ops = [TransformZToX, TransformZToY]; let ops = [TransformZToX, TransformZToY];
for (op in ops) { for op in ops {
within { within {
ApplyBasisChange([op, op], [qubitP], qubitQ); ApplyBasisChange([op, op], [qubitP], qubitQ);
ApplyCNOTChainWithTarget([qubitP], qubitQ); ApplyCNOTChainWithTarget([qubitP], qubitQ);
@ -247,7 +245,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
/// Qubit that determines the sign of time-evolution. /// Qubit that determines the sign of time-evolution.
/// ## qubits /// ## qubits
/// Qubits of Hamiltonian. /// Qubits of Hamiltonian.
operation _JWOptimizedPQandPQQRTerm(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[]) internal operation _JWOptimizedPQandPQQRTerm(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[])
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
let ((idxTermType, coeff), idxFermions) = term!; let ((idxTermType, coeff), idxFermions) = term!;
let angle = (1.0 * coeff[0]) * stepSize; let angle = (1.0 * coeff[0]) * stepSize;
@ -283,7 +281,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
/// Qubit that determines the sign of time-evolution. /// Qubit that determines the sign of time-evolution.
/// ## qubits /// ## qubits
/// Qubits of Hamiltonian. /// Qubits of Hamiltonian.
operation _JWOptimized0123Term(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[]) internal operation _JWOptimized0123Term(term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[])
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
let ((idxTermType, coeff), idxFermions) = term!; let ((idxTermType, coeff), idxFermions) = term!;
ApplyWithCA( ApplyWithCA(
@ -296,7 +294,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
/// # Summary /// # Summary
/// Implementation step of `JWOptimized0123Term_`; /// Implementation step of `JWOptimized0123Term_`;
operation _JWOptimized0123TermImpl (term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[]) internal operation _JWOptimized0123TermImpl (term : GeneratorIndex, stepSize : Double, parityQubit : Qubit, qubits : Qubit[])
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
let x = TransformZToX; let x = TransformZToX;
let y = TransformZToY; let y = TransformZToY;
@ -318,7 +316,7 @@ namespace Microsoft.Quantum.Research.Chemistry {
let qubitsPQR = qubitsPQRS[0 .. Length(qubitsPQRS) - 2]; let qubitsPQR = qubitsPQRS[0 .. Length(qubitsPQRS) - 2];
let qubitS = qubitsPQRS[3]; let qubitS = qubitsPQRS[3];
for (idxOp in 0 .. 7) { for idxOp in 0 .. 7 {
if (IsNotZero(v0123[idxOp % 4])) { if (IsNotZero(v0123[idxOp % 4])) {
let op0 = _JWOptimizedZ(angle * v0123[idxOp % 4], parityQubit, _); let op0 = _JWOptimizedZ(angle * v0123[idxOp % 4], parityQubit, _);
let op1 = ApplyWithCA(ApplyCNOTChainWithTarget(qubitsPQR, _), op0, _); let op1 = ApplyWithCA(ApplyCNOTChainWithTarget(qubitsPQR, _), op0, _);

Просмотреть файл

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2003.3107"> <Project Sdk="Microsoft.Quantum.Sdk/0.16.2104138035">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
@ -19,7 +19,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Quantum.Chemistry" Version="0.11.2003.3107" /> <PackageReference Include="Microsoft.Quantum.Chemistry" Version="0.16.2104138035" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

Просмотреть файл

@ -10,510 +10,481 @@ namespace SystemTestJWOptimizedES {
open Microsoft.Quantum.Chemistry.JordanWigner; open Microsoft.Quantum.Chemistry.JordanWigner;
open Microsoft.Quantum.Research.Chemistry; open Microsoft.Quantum.Research.Chemistry;
open Microsoft.Quantum.Measurement; open Microsoft.Quantum.Measurement;
open Microsoft.Quantum.Diagnostics as Diag;
// Test phase of PP term. // Test phase of PP term.
operation PPTermFromGeneralHamiltonianTestOp (identity : Double, data : JWOptimizedHTerms) : Unit { operation PPTermFromGeneralHamiltonianTestOp (identity : Double, data : JWOptimizedHTerms) : Unit {
let time = 1.0; let time = 1.0;
using ((qubits, parityQubit) = (Qubit[3], Qubit())) { use qubits = Qubit[3];
use parityQubit = Qubit();
// Create |00> + |10>
let qubitSys = qubits[0 .. Length(qubits) - 2];
H(qubits[0]);
JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubitSys);
AssertPhase(-0.5 * time, qubits[0], 1E-10);
ResetAll(qubits);
// Create |00> + |01> // Create |00> + |10>
H(qubits[1]); let qubitSys = qubits[0 .. Length(qubits) - 2];
JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubitSys); H(qubits[0]);
AssertPhase(-0.0 * time, qubits[1], 1E-10); JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubitSys);
ResetAll(qubits); Diag.AssertPhase(-0.5 * time, qubits[0], 1E-10);
let ctrl = qubits[Length(qubits) - 1]; ResetAll(qubits);
H(ctrl);
X(qubitSys[0]); // Create |00> + |01>
(Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubits[0]]); H(qubits[1]);
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitSys); JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubitSys);
AssertPhase(-0.5 * time, ctrl, 1E-10); Diag.AssertPhase(-0.0 * time, qubits[1], 1E-10);
ResetAll(qubits + [parityQubit]); ResetAll(qubits);
} let ctrl = qubits[Length(qubits) - 1];
H(ctrl);
X(qubitSys[0]);
(Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubits[0]]);
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitSys);
Diag.AssertPhase(-0.5 * time, ctrl, 1E-10);
ResetAll(qubits + [parityQubit]);
} }
// Test hopping from site A to site B. // Test hopping from site A to site B.
operation PQTermABFromGeneralHamiltonianTestOp (data : JWOptimizedHTerms) : Unit { operation PQTermABFromGeneralHamiltonianTestOp (data : JWOptimizedHTerms) : Unit {
// Test probability // Test probability
let time = 0.5 * PI(); let time = 0.5 * PI();
mutable arrPrep = new Int[4]; mutable arrPrep = new Int[4];
set arrPrep = [0, 1, 2, 3]; set arrPrep = [0, 1, 2, 3];
mutable arrMeasure = new Int[4]; mutable arrMeasure = new Int[4];
set arrMeasure = [1, 0, 2, 3]; set arrMeasure = [1, 0, 2, 3];
use parityQubit = Qubit();
using (parityQubit = Qubit[1]) {
using (qubits = Qubit[4]) { use qubits = Qubit[4] {
// Create |1000>
// Create |1000> for idx in 0 .. 3 {
for (idx in 0 .. 3) { X(qubits[arrPrep[idx]]);
X(qubits[arrPrep[idx]]); JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubits);
JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], qubits); Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[idx]]], One, 1.0, "PQTermTest probability failed.", 1E-10);
AssertProb([PauliZ], [qubits[arrMeasure[idx]]], One, 1.0, "PQTermTest probability failed.", 1E-10); ResetAll(qubits + [parityQubit]);
ResetAll(qubits + parityQubit);
}
}
// Test phase
using (qubitsC = Qubit[5]) {
// Create |1000>(|0>+|1>)
let ctrl = qubitsC[4];
H(ctrl);
X(qubitsC[arrPrep[0]]);
// Create |1000>|0>+ i|0100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitsC[0 .. 3]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |1000>|0>+ i|1000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[1]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
AssertPhase(-0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + parityQubit);
} }
} }
// Test phase
use qubitsC = Qubit[5] {
// Create |1000>(|0>+|1>)
let ctrl = qubitsC[4];
H(ctrl);
X(qubitsC[arrPrep[0]]);
// Create |1000>|0>+ i|0100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitsC[0 .. 3]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |1000>|0>+ i|1000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[1]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
Diag.AssertPhase(-0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + [parityQubit]);
}
} }
// Test hopping from site A to site C // Test hopping from site A to site C
operation PQTermACFromGeneralHamiltonianTestOp (data : JWOptimizedHTerms) : Unit { operation PQTermACFromGeneralHamiltonianTestOp (data : JWOptimizedHTerms) : Unit {
// Test probability // Test probability
let time = 0.5 * PI(); let time = 0.5 * PI();
mutable arrPrep = new Int[4]; mutable arrPrep = new Int[4];
set arrPrep = [0, 2, 3, 5]; set arrPrep = [0, 2, 3, 5];
mutable arrMeasure = new Int[4]; mutable arrMeasure = new Int[4];
set arrMeasure = [2, 0, 3, 5]; set arrMeasure = [2, 0, 3, 5];
using (parityQubit = Qubit[1]) { use parityQubit = Qubit();
use qubits = Qubit[6] {
using (qubits = Qubit[6]) { // Create |100000>
for idx in 0 .. 3 {
// Create |100000> X(qubits[arrPrep[idx]]);
for (idx in 0 .. 3) { JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubits);
X(qubits[arrPrep[idx]]); Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[idx]]], One, 1.0, "PQTermTest probability failed.", 1E-10);
JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], qubits); ResetAll(qubits + [parityQubit]);
AssertProb([PauliZ], [qubits[arrMeasure[idx]]], One, 1.0, "PQTermTest probability failed.", 1E-10);
ResetAll(qubits + parityQubit);
}
}
// Test phase
using (qubitsC = Qubit[7]) {
// Create |1000>(|0>+|1>)
let ctrl = qubitsC[6];
H(ctrl);
X(qubitsC[arrPrep[0]]);
// Create |10000>|0>+ i|00100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitsC[0 .. 5]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |10000>|0>+ i|10000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[2]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
AssertPhase(-0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + parityQubit);
H(ctrl);
X(qubitsC[arrPrep[0]]);
X(qubitsC[1]);
// Create |11000>|0>- i|01100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitsC[0 .. 5]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |11000>|0>- i|11000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[2]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
AssertPhase(0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + parityQubit);
} }
} }
// Test phase
use qubitsC = Qubit[7] {
// Create |1000>(|0>+|1>)
let ctrl = qubitsC[6];
H(ctrl);
X(qubitsC[arrPrep[0]]);
// Create |10000>|0>+ i|00100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitsC[0 .. 5]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |10000>|0>+ i|10000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[2]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
Diag.AssertPhase(-0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + [parityQubit]);
H(ctrl);
X(qubitsC[arrPrep[0]]);
X(qubitsC[1]);
// Create |11000>|0>- i|01100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitsC[0 .. 5]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |11000>|0>- i|11000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[2]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
Diag.AssertPhase(0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + [parityQubit]);
}
} }
// Test phase of PP term. // Test phase of PP term.
operation PQQPTermFromGeneralHamiltonianTestOp (identity : Double, data : JWOptimizedHTerms) : Unit { operation PQQPTermFromGeneralHamiltonianTestOp (identity : Double, data : JWOptimizedHTerms) : Unit {
let time = 1.0; let time = 1.0;
using (qubits = Qubit[5]) { use qubits = Qubit[5];
use parityQubit = Qubit();
using (parityQubit = Qubit[1]) {
let ctrl = qubits[Length(qubits) - 1]; let ctrl = qubits[Length(qubits) - 1];
let qubitSys = qubits[0 .. Length(qubits) - 2]; let qubitSys = qubits[0 .. Length(qubits) - 2];
// Create |1100>(|0>+|1>); // Create |1100>(|0>+|1>);
H(ctrl); H(ctrl);
X(qubitSys[0]); X(qubitSys[0]);
X(qubitSys[1]); X(qubitSys[1]);
// Create |1100>(|0>+e^{i t}|1>); // Create |1100>(|0>+e^{i t}|1>);
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitSys); (Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitSys);
(Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]); (Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]);
AssertPhase(-0.5 * time, ctrl, 1E-10); Diag.AssertPhase(-0.5 * time, ctrl, 1E-10);
ResetAll(qubits + parityQubit); ResetAll(qubits + [parityQubit]);
// Create |1110>(|0>+|1>); // Create |1110>(|0>+|1>);
H(ctrl); H(ctrl);
X(qubitSys[0]); X(qubitSys[0]);
X(qubitSys[1]); X(qubitSys[1]);
X(qubitSys[2]); X(qubitSys[2]);
// Create |1100>(|0>+e^{i t}|1>); // Create |1100>(|0>+e^{i t}|1>);
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitSys); (Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitSys);
(Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]); (Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]);
AssertPhase(-0.5 * time, ctrl, 1E-10); Diag.AssertPhase(-0.5 * time, ctrl, 1E-10);
ResetAll(qubits + parityQubit); ResetAll(qubits + [parityQubit]);
// Create |1111>(|0>+|1>); // Create |1111>(|0>+|1>);
H(ctrl); H(ctrl);
X(qubitSys[0]); X(qubitSys[0]);
X(qubitSys[1]); X(qubitSys[1]);
X(qubitSys[2]); X(qubitSys[2]);
X(qubitSys[3]); X(qubitSys[3]);
// Create |1100>(|0>+e^{i t}|1>); // Create |1100>(|0>+e^{i t}|1>);
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitSys); (Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitSys);
(Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]); (Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]);
AssertPhase(-0.5 * time, ctrl, 1E-10); Diag.AssertPhase(-0.5 * time, ctrl, 1E-10);
ResetAll(qubits + parityQubit); ResetAll(qubits + [parityQubit]);
}
}
} }
operation PQQRTermFromGeneralHamiltonianTestOp (data : JWOptimizedHTerms) : Unit { operation PQQRTermFromGeneralHamiltonianTestOp (data : JWOptimizedHTerms) : Unit {
// Test probability // Test probability
mutable time = (4.0 * 0.5) * PI(); mutable time = (4.0 * 0.5) * PI();
mutable arrPrep = new Int[4]; mutable arrPrep = new Int[4];
set arrPrep = [0, 2, 1, 3]; set arrPrep = [0, 2, 1, 3];
mutable arrMeasure = new Int[4]; mutable arrMeasure = new Int[4];
set arrMeasure = [2, 0, 3, 1]; set arrMeasure = [2, 0, 3, 1];
using (qubits = Qubit[4]) { use qubits = Qubit[4];
use parityQubit = Qubit();
using (parityQubit = Qubit[1]) {
// Create |1000>
// Create |1000> for idx in 0 .. 3 {
for (idx in 0 .. 3) { X(qubits[arrPrep[idx]]);
X(qubits[arrPrep[idx]]); JWOptimizedApplyTrotterStep(data, time, 0.1 * time, parityQubit, qubits);
JWOptimizedApplyTrotterStep(data, time, 0.1 * time, parityQubit[0], qubits); Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[idx]]], One, 0.0, "PQQRTermTest a probability failed.", 1E-10);
AssertProb([PauliZ], [qubits[arrMeasure[idx]]], One, 0.0, "PQQRTermTest a probability failed.", 1E-10); ResetAll(qubits);
ResetAll(qubits);
}
// Create |1100>
set time = PI();
X(qubits[arrPrep[0]]);
X(qubits[1]);
JWOptimizedApplyTrotterStep(data, time, 0.1 * time, parityQubit[0], qubits);
AssertProb([PauliZ], [qubits[arrMeasure[0]]], One, 1.0, "PQQRTermTest b probability failed.", 0.01);
ResetAll(qubits);
// Create |1001>
set time = PI();
X(qubits[arrPrep[0]]);
X(qubits[3]);
JWOptimizedApplyTrotterStep(data, time, 0.1 * time, parityQubit[0], qubits);
AssertProb([PauliZ], [qubits[arrMeasure[0]]], One, 0.0, "PQQRTermTest c probability failed.", 0.01);
ResetAll(qubits);
}
} }
// Create |1100>
set time = PI();
X(qubits[arrPrep[0]]);
X(qubits[1]);
JWOptimizedApplyTrotterStep(data, time, 0.1 * time, parityQubit, qubits);
Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[0]]], One, 1.0, "PQQRTermTest b probability failed.", 0.01);
ResetAll(qubits);
// Create |1001>
set time = PI();
X(qubits[arrPrep[0]]);
X(qubits[3]);
JWOptimizedApplyTrotterStep(data, time, 0.1 * time, parityQubit, qubits);
Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[0]]], One, 0.0, "PQQRTermTest c probability failed.", 0.01);
ResetAll(qubits);
} }
operation PQRSTermFromGeneralHamiltonianTestOp (data : JWOptimizedHTerms) : Unit { operation PQRSTermFromGeneralHamiltonianTestOp (data : JWOptimizedHTerms) : Unit {
// Test probability // Test probability
mutable time = 1.0; mutable time = 1.0;
mutable prob = Sin(time) * Sin(time); mutable prob = Sin(time) * Sin(time);
using (qubits = Qubit[4]) { use qubits = Qubit[4];
use parityQubit = Qubit();
using (parityQubit = Qubit[1]) {
// Create |1100>
// Create |1100> X(qubits[0]);
X(qubits[0]); X(qubits[1]);
X(qubits[1]); JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubits);
JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], qubits); Diag.AssertMeasurementProbability([PauliZ], [qubits[2]], One, prob, "PQRSTermTest a probability failed.", 1E-10);
AssertProb([PauliZ], [qubits[2]], One, prob, "PQRSTermTest a probability failed.", 1E-10); Diag.AssertMeasurementProbability([PauliZ], [qubits[3]], One, prob, "PQRSTermTest b probability failed.", 1E-10);
AssertProb([PauliZ], [qubits[3]], One, prob, "PQRSTermTest b probability failed.", 1E-10); ResetAll(qubits + [parityQubit]);
ResetAll(qubits + parityQubit);
}
}
} }
// Test phase of PP term. // Test phase of PP term.
operation PPTermFromLiquidOrbitalTestOp (identity : Double, data : JWOptimizedHTerms) : Unit { operation PPTermFromLiquidOrbitalTestOp (identity : Double, data : JWOptimizedHTerms) : Unit {
let time = 1.0; let time = 1.0;
using (qubits = Qubit[3]) { use qubits = Qubit[3];
use parityQubit = Qubit();
using (parityQubit = Qubit[1]) {
// Create |00> + |10>
// Create |00> + |10> let qubitSys = qubits[0 .. Length(qubits) - 2];
let qubitSys = qubits[0 .. Length(qubits) - 2]; H(qubits[0]);
H(qubits[0]); JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubitSys);
JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], qubitSys); Diag.AssertPhase(-0.5 * time, qubits[0], 1E-10);
AssertPhase(-0.5 * time, qubits[0], 1E-10); ResetAll(qubits + [parityQubit]);
ResetAll(qubits + parityQubit);
// Create |00> + |01>
// Create |00> + |01> H(qubits[1]);
H(qubits[1]); JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubitSys);
JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], qubitSys); Diag.AssertPhase(-0.5 * time, qubits[1], 1E-10);
AssertPhase(-0.5 * time, qubits[1], 1E-10); ResetAll(qubits + [parityQubit]);
ResetAll(qubits + parityQubit); let ctrl = qubits[Length(qubits) - 1];
let ctrl = qubits[Length(qubits) - 1]; H(ctrl);
H(ctrl); X(qubitSys[0]);
X(qubitSys[0]); (Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubits[0]]);
(Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubits[0]]); (Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitSys);
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitSys); Diag.AssertPhase(-0.5 * time, ctrl, 1E-10);
AssertPhase(-0.5 * time, ctrl, 1E-10); ResetAll(qubits + [parityQubit]);
ResetAll(qubits + parityQubit);
}
}
} }
// Test hopping from site A to site B. // Test hopping from site A to site B.
operation PQTermABFromLiquidOrbitalTestOp (data : JWOptimizedHTerms) : Unit { operation PQTermABFromLiquidOrbitalTestOp (data : JWOptimizedHTerms) : Unit {
// Test probability // Test probability
let time = 0.5 * PI(); let time = 0.5 * PI();
mutable arrPrep = new Int[4]; mutable arrPrep = new Int[4];
set arrPrep = [0, 1, 2, 3]; set arrPrep = [0, 1, 2, 3];
mutable arrMeasure = new Int[4]; mutable arrMeasure = new Int[4];
set arrMeasure = [1, 0, 3, 2]; set arrMeasure = [1, 0, 3, 2];
using (parityQubit = Qubit[1]) { use parityQubit = Qubit();
using (qubits = Qubit[4]) { use qubits = Qubit[4] {
// Create |1000> // Create |1000>
for (idx in 0 .. 3) { for idx in 0 .. 3 {
X(qubits[arrPrep[idx]]); X(qubits[arrPrep[idx]]);
JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], qubits); JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubits);
AssertProb([PauliZ], [qubits[arrMeasure[idx]]], One, 1.0, "PQTermTest probability failed.", 1E-10); Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[idx]]], One, 1.0, "PQTermTest probability failed.", 1E-10);
ResetAll(qubits + parityQubit); ResetAll(qubits + [parityQubit]);
}
}
// Test phase
using (qubitsC = Qubit[5]) {
// Create |1000>(|0>+|1>)
let ctrl = qubitsC[4];
H(ctrl);
X(qubitsC[arrPrep[0]]);
// Create |1000>|0>+ i|0100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitsC[0 .. 3]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |1000>|0>+ i|1000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[1]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
AssertPhase(-0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + parityQubit);
} }
} }
// Test phase
use qubitsC = Qubit[5] {
// Create |1000>(|0>+|1>)
let ctrl = qubitsC[4];
H(ctrl);
X(qubitsC[arrPrep[0]]);
// Create |1000>|0>+ i|0100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitsC[0 .. 3]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |1000>|0>+ i|1000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[1]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
Diag.AssertPhase(-0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + [parityQubit]);
}
} }
// Test hopping from site A to site C // Test hopping from site A to site C
operation PQTermACFromLiquidOrbitalTestOp (data : JWOptimizedHTerms) : Unit { operation PQTermACFromLiquidOrbitalTestOp (data : JWOptimizedHTerms) : Unit {
// Test probability // Test probability
let time = 0.5 * PI(); let time = 0.5 * PI();
mutable arrPrep = new Int[4]; mutable arrPrep = new Int[4];
set arrPrep = [0, 2, 3, 5]; set arrPrep = [0, 2, 3, 5];
mutable arrMeasure = new Int[4]; mutable arrMeasure = new Int[4];
set arrMeasure = [2, 0, 5, 3]; set arrMeasure = [2, 0, 5, 3];
using (parityQubit = Qubit[1]) { use parityQubit = Qubit();
using (qubits = Qubit[6]) { use qubits = Qubit[6] {
// Create |100000>
// Create |100000> for idx in 0 .. 3 {
for (idx in 0 .. 3) { X(qubits[arrPrep[idx]]);
X(qubits[arrPrep[idx]]); JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubits);
JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], qubits); Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[idx]]], One, 1.0, "PQTermTest probability failed.", 1E-10);
AssertProb([PauliZ], [qubits[arrMeasure[idx]]], One, 1.0, "PQTermTest probability failed.", 1E-10); ResetAll(qubits + [parityQubit]);
ResetAll(qubits + parityQubit);
}
}
// Test phase
using (qubitsC = Qubit[7]) {
// Create |1000>(|0>+|1>)
let ctrl = qubitsC[6];
H(ctrl);
X(qubitsC[arrPrep[0]]);
// Create |10000>|0>+ i|00100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitsC[0 .. 5]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |10000>|0>+ i|10000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[2]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
AssertPhase(-0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + parityQubit);
H(ctrl);
X(qubitsC[arrPrep[0]]);
X(qubitsC[1]);
// Create |11000>|0>- i|01100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitsC[0 .. 5]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |11000>|0>- i|11000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[2]);
AssertProb([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
AssertPhase(0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + parityQubit);
} }
} }
// Test phase
use qubitsC = Qubit[7] {
// Create |1000>(|0>+|1>)
let ctrl = qubitsC[6];
H(ctrl);
X(qubitsC[arrPrep[0]]);
// Create |10000>|0>+ i|00100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitsC[0 .. 5]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |10000>|0>+ i|10000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[2]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
Diag.AssertPhase(-0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + [parityQubit]);
H(ctrl);
X(qubitsC[arrPrep[0]]);
X(qubitsC[1]);
// Create |11000>|0>- i|01100>|1>
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitsC[0 .. 5]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.5, "PQTermTest probability failed.", 1E-10);
// Create |11000>|0>- i|11000>|1>
CNOT(ctrl, qubitsC[0]);
CNOT(ctrl, qubitsC[2]);
Diag.AssertMeasurementProbability([PauliZ], [qubitsC[arrMeasure[0]]], One, 0.0, "PQTermTest probability failed.", 1E-10);
Diag.AssertPhase(0.25 * PI(), ctrl, 1E-10);
ResetAll(qubitsC + [parityQubit]);
}
} }
// Test phase of PP term. // Test phase of PP term.
operation PQQPTermFromLiquidOrbitalTestOp (identity : Double, data : JWOptimizedHTerms) : Unit { operation PQQPTermFromLiquidOrbitalTestOp (identity : Double, data : JWOptimizedHTerms) : Unit {
let time = 1.0; let time = 1.0;
using (parityQubit = Qubit[1]) { use parityQubit = Qubit();
use qubits = Qubit[5];
using (qubits = Qubit[5]) { let ctrl = qubits[Length(qubits) - 1];
let ctrl = qubits[Length(qubits) - 1]; let qubitSys = qubits[0 .. Length(qubits) - 2];
let qubitSys = qubits[0 .. Length(qubits) - 2];
// Create |1100>(|0>+|1>);
// Create |1100>(|0>+|1>); H(ctrl);
H(ctrl); X(qubitSys[0]);
X(qubitSys[0]); X(qubitSys[1]);
X(qubitSys[1]);
// Create |1100>(|0>+e^{i t / 2}|1>);
// Create |1100>(|0>+e^{i t / 2}|1>); (Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitSys);
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitSys); (Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]);
(Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]); Diag.AssertPhase(-0.5 * time, ctrl, 1E-10);
AssertPhase(-0.5 * time, ctrl, 1E-10); ResetAll(qubits);
ResetAll(qubits);
// Create |1110>(|0>+|1>);
// Create |1110>(|0>+|1>); H(ctrl);
H(ctrl); X(qubitSys[0]);
X(qubitSys[0]); X(qubitSys[1]);
X(qubitSys[1]); X(qubitSys[2]);
X(qubitSys[2]);
// Create |1100>(|0>+e^{i 2 t}|1>);
// Create |1100>(|0>+e^{i 2 t}|1>); (Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitSys);
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitSys); (Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]);
(Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]); Diag.AssertPhase(-1.0 * time, ctrl, 1E-10);
AssertPhase(-1.0 * time, ctrl, 1E-10); ResetAll(qubits);
ResetAll(qubits);
// Create |1111>(|0>+|1>);
// Create |1111>(|0>+|1>); H(ctrl);
H(ctrl); X(qubitSys[0]);
X(qubitSys[0]); X(qubitSys[1]);
X(qubitSys[1]); X(qubitSys[2]);
X(qubitSys[2]); X(qubitSys[3]);
X(qubitSys[3]);
// Create |1100>(|0>+e^{i 4 t}|1>);
// Create |1100>(|0>+e^{i 4 t}|1>); (Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit, _)))([ctrl], qubitSys);
(Controlled (JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], _)))([ctrl], qubitSys); (Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]);
(Controlled (Exp([PauliI], time * identity, _)))([ctrl], [qubitSys[0]]); Diag.AssertPhase(-2.0 * time, ctrl, 1E-10);
AssertPhase(-2.0 * time, ctrl, 1E-10); ResetAll(qubits);
ResetAll(qubits);
}
}
} }
operation PQQRTermFromLiquidOrbitalTestOp (data : JWOptimizedHTerms) : Unit { operation PQQRTermFromLiquidOrbitalTestOp (data : JWOptimizedHTerms) : Unit {
// Test probability // Test probability
mutable time = (4.0 * 0.5) * PI(); mutable time = (4.0 * 0.5) * PI();
mutable arrPrep = new Int[4]; mutable arrPrep = new Int[4];
set arrPrep = [0, 1, 2, 3]; set arrPrep = [0, 1, 2, 3];
mutable arrMeasure = new Int[4]; mutable arrMeasure = new Int[4];
set arrMeasure = [1, 0, 3, 2]; set arrMeasure = [1, 0, 3, 2];
using (parityQubit = Qubit[1]) { use parityQubit = Qubit();
use qubits = Qubit[4];
using (qubits = Qubit[4]) {
// Create |1000>
// Create |1000> for idx in 0 .. 3 {
for (idx in 0 .. 3) { X(qubits[arrPrep[idx]]);
X(qubits[arrPrep[idx]]); JWOptimizedApplyTrotterStep(data, time, 0.1 * time, parityQubit, qubits);
JWOptimizedApplyTrotterStep(data, time, 0.1 * time, parityQubit[0], qubits); Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[idx]]], One, 0.0, "PQQRTermTest a probability failed.", 1E-10);
AssertProb([PauliZ], [qubits[arrMeasure[idx]]], One, 0.0, "PQQRTermTest a probability failed.", 1E-10); ResetAll(qubits);
ResetAll(qubits);
}
// Create |1010>
set time = (0.5 * PI()) / Sqrt(2.0);
X(qubits[arrPrep[0]]);
X(qubits[2]);
JWOptimizedApplyTrotterStep(data, time, 0.01 * time, parityQubit[0], qubits);
AssertProb([PauliZ], [qubits[arrMeasure[0]]], One, 0.5, "PQQRTermTest b probability failed.", 0.01);
ResetAll(qubits);
// Create |1001>
set time = (0.5 * PI()) / Sqrt(2.0);
X(qubits[arrPrep[0]]);
X(qubits[3]);
JWOptimizedApplyTrotterStep(data, time, 0.01 * time, parityQubit[0], qubits);
AssertProb([PauliZ], [qubits[arrMeasure[0]]], One, 0.25, "PQQRTermTest c probability failed.", 0.01);
ResetAll(qubits);
}
} }
// Create |1010>
set time = (0.5 * PI()) / Sqrt(2.0);
X(qubits[arrPrep[0]]);
X(qubits[2]);
JWOptimizedApplyTrotterStep(data, time, 0.01 * time, parityQubit, qubits);
Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[0]]], One, 0.5, "PQQRTermTest b probability failed.", 0.01);
ResetAll(qubits);
// Create |1001>
set time = (0.5 * PI()) / Sqrt(2.0);
X(qubits[arrPrep[0]]);
X(qubits[3]);
JWOptimizedApplyTrotterStep(data, time, 0.01 * time, parityQubit, qubits);
Diag.AssertMeasurementProbability([PauliZ], [qubits[arrMeasure[0]]], One, 0.25, "PQQRTermTest c probability failed.", 0.01);
ResetAll(qubits);
} }
operation PQRSTermFromLiquidOrbitalTestOp (data : JWOptimizedHTerms) : Unit { operation PQRSTermFromLiquidOrbitalTestOp (data : JWOptimizedHTerms) : Unit {
// Test probability // Test probability
mutable time = 1.0; mutable time = 1.0;
mutable prob = Sin(time) * Sin(time); mutable prob = Sin(time) * Sin(time);
using (parityQubit = Qubit[1]) { use parityQubit = Qubit();
use qubits = Qubit[4];
using (qubits = Qubit[4]) {
// Create |1010>
// Create |1010> X(qubits[0]);
X(qubits[0]); X(qubits[2]);
X(qubits[2]); JWOptimizedApplyTrotterStep(data, time, time, parityQubit, qubits);
JWOptimizedApplyTrotterStep(data, time, time, parityQubit[0], qubits); Diag.AssertMeasurementProbability([PauliZ], [qubits[1]], One, prob, "PQRSTermTest a probability failed.", 1E-10);
AssertProb([PauliZ], [qubits[1]], One, prob, "PQRSTermTest a probability failed.", 1E-10); Diag.AssertMeasurementProbability([PauliZ], [qubits[3]], One, prob, "PQRSTermTest b probability failed.", 1E-10);
AssertProb([PauliZ], [qubits[3]], One, prob, "PQRSTermTest b probability failed.", 1E-10); ResetAll(qubits);
ResetAll(qubits);
}
}
} }
operation JWOptimizedApplyTrotterStep (data : JWOptimizedHTerms, time : Double, trotterStepSize : Double, parityQubit : Qubit, qubits : Qubit[]) operation JWOptimizedApplyTrotterStep (data : JWOptimizedHTerms, time : Double, trotterStepSize : Double, parityQubit : Qubit, qubits : Qubit[])
: Unit is Adj + Ctl { : Unit is Adj + Ctl {
let generatorSystem = JWOptimizedGeneratorSystem(data); let generatorSystem = JWOptimizedGeneratorSystem(data);
@ -522,7 +493,5 @@ namespace SystemTestJWOptimizedES {
let simulationAlgorithm = TrotterSimulationAlgorithm(trotterStepSize, trotterOrder); let simulationAlgorithm = TrotterSimulationAlgorithm(trotterStepSize, trotterOrder);
simulationAlgorithm!(time, evolutionGenerator, qubits); simulationAlgorithm!(time, evolutionGenerator, qubits);
} }
} }

Просмотреть файл

@ -23,7 +23,7 @@ namespace Microsoft.Quantum.Research.Tests {
repeat { repeat {
mutable actual = 0.0; mutable actual = 0.0;
using (eigenstate = Qubit()) { use eigenstate = Qubit() {
X(eigenstate); X(eigenstate);
set actual = RandomWalkPhaseEstimation(0.0, 1.0, 61, 100000, 0, oracle, [eigenstate]); set actual = RandomWalkPhaseEstimation(0.0, 1.0, 61, 100000, 0, oracle, [eigenstate]);
Reset(eigenstate); Reset(eigenstate);
@ -34,7 +34,8 @@ namespace Microsoft.Quantum.Research.Tests {
} else { } else {
return (); return ();
} }
} until (nAttemptsSoFar >= nAttemptsAllowed); }
until nAttemptsSoFar >= nAttemptsAllowed;
fail $"CheckBayesianPERandomWalk failed {nAttemptsSoFar} times in a row."; fail $"CheckBayesianPERandomWalk failed {nAttemptsSoFar} times in a row.";
} }