Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
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
42 changes: 17 additions & 25 deletions Standard/tests/AmplitudeAmplificationTests.qs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,31 @@ namespace Microsoft.Quantum.Tests {
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Oracles;

///Here we consider the smallest example of amplitude amplification
///Suppose we have a single-qubit oracle that prepares the state
/// O |0> = \lambda |1> + \sqrt{1-|\lambda|^2} |0>
/// The goal is to amplify the |1> state
/// We can do this either by synthesizing the reflection about the start and target states ourselves,
/// We can also do it by passing the oracle for state preparation
operation ExampleStatePrepImpl (lambda : Double, idxFlagQubit : Int, qubitStart : Qubit[]) : Unit is Adj + Ctl {
// Here we consider the smallest example of amplitude amplification
// Suppose we have a single-qubit oracle that prepares the state
// O |0> = \lambda |1> + \sqrt{1-|\lambda|^2} |0>
// The goal is to amplify the |1> state
// We can do this either by synthesizing the reflection about the start and target states ourselves,
// We can also do it by passing the oracle for state preparation
internal operation ExampleStatePrepImpl(lambda : Double, idxFlagQubit : Int, qubitStart : Qubit[]) : Unit is Adj + Ctl {
let rotAngle = 2.0 * ArcSin(lambda);
Ry(rotAngle, qubitStart[idxFlagQubit]);
}


function ExampleStatePrep (lambda : Double) : StateOracle {

internal function ExampleStatePrep(lambda : Double) : StateOracle {
return StateOracle(ExampleStatePrepImpl(lambda, _, _));
}


/// In this minimal example, there are no system qubits, only a single flag qubit.
/// ExampleStatePrep is already of type StateOracle, so we call
/// StandardAmplitudeAmplification(iterations: Int, stateOracle : StateOracle, idxFlagQubit : Int startQubits: Qubit[]) : ()
// In this minimal example, there are no system qubits, only a single flag qubit.
// ExampleStatePrep is already of type StateOracle, so we call
// StandardAmplitudeAmplification(iterations: Int, stateOracle : StateOracle, idxFlagQubit : Int startQubits: Qubit[]) : ()
@Test("QuantumSimulator")
operation CheckAmpAmpByOracle () : Unit {

operation CheckAmpAmpByOracle() : Unit {
use qubits = Qubit[1];
ResetAll(qubits);

for nIterations in 0 .. 5 {

for idx in 1 .. 20 {
let lambda = IntAsDouble(idx) / 20.0;
let rotAngle = ArcSin(lambda);
Expand All @@ -50,16 +46,15 @@ namespace Microsoft.Quantum.Tests {
(StandardAmplitudeAmplification(nIterations, stateOracle, idxFlag))(startQubits);
let successAmplitude = Sin(IntAsDouble(2 * nIterations + 1) * rotAngle);
let successProbability = successAmplitude * successAmplitude;
AssertMeasurementProbability([PauliZ], [startQubits[idxFlag]], One, successProbability, $"Error: Success probability does not match theory", 1E-10);
AssertMeasurementProbability([PauliZ], [startQubits[idxFlag]], One, successProbability, "Error: Success probability does not match theory", 1E-10);
ResetAll(qubits);
}
}
}

@Test("QuantumSimulator")
operation CheckAmpAmpObliviousByOraclePhases () : Unit {
operation CheckAmpAmpObliviousByOraclePhases() : Unit {
use qubits = Qubit[1];
ResetAll(qubits);

for nIterations in 0 .. 5 {
let phases = StandardReflectionPhases(nIterations);
Expand All @@ -74,24 +69,23 @@ namespace Microsoft.Quantum.Tests {
(ObliviousAmplitudeAmplificationFromStatePreparation(phases, ancillaOracle, signalOracle, idxFlag))(auxRegister, systemRegister);
let successAmplitude = Sin((IntAsDouble(2 * nIterations + 1) * rotAngle) * 0.5);
let successProbability = successAmplitude * successAmplitude;
AssertMeasurementProbability([PauliZ], [auxRegister[idxFlag]], One, successProbability, $"Error: Success probability does not match theory", 1E-10);
AssertMeasurementProbability([PauliZ], [auxRegister[idxFlag]], One, successProbability, "Error: Success probability does not match theory", 1E-10);
ResetAll(qubits);
}
}
}

@Test("QuantumSimulator")
operation CheckAmpAmpTargetStateReflectionOracle () : Unit {
operation CheckAmpAmpTargetStateReflectionOracle() : Unit {
use qubits = Qubit[1];
ResetAll(qubits);

for idx in 0 .. 20 {
let rotangle = (IntAsDouble(idx) * PI()) / 20.0;
let targetStateReflection = TargetStateReflectionOracle(0);
let success = Cos(0.5 * rotangle) * Cos(0.5 * rotangle);
H(qubits[0]);
targetStateReflection!(rotangle, qubits);
AssertMeasurementProbability([PauliX], qubits, Zero, success, $"Error: Success probability does not match theory", 1E-10);
AssertMeasurementProbability([PauliX], qubits, Zero, success, "Error: Success probability does not match theory", 1E-10);
ResetAll(qubits);
}
}
Expand All @@ -109,5 +103,3 @@ namespace Microsoft.Quantum.Tests {
}

}