-
Notifications
You must be signed in to change notification settings - Fork 137
Quantum Phase estimation sample via ApplyQPE #2506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/// # Sample | ||
/// Quantum Phase Estimation | ||
/// | ||
/// # Description | ||
/// This sample demonstrates how to use Quantum Phase Estimation (QPE) algorithm | ||
/// to estimate the eigenvalue of a specific unitary operation. | ||
/// QPE algorithm is performed by calling `ApplyQPE` library operation. | ||
/// Run this sample and check the histogram of results. | ||
|
||
import Std.Math.*; | ||
|
||
/// # Summary | ||
/// Estimate the the eigenvalue of a specific unitary U for its eigenvector |010⟩ | ||
/// using `ApplyQPE`. The result is returned as a complex polar value. | ||
operation Main() : ComplexPolar { | ||
// Allocate qubits to be used in phase estimation | ||
use state = Qubit[3]; | ||
|
||
// Prepare U eigenvector |010⟩ | ||
X(state[1]); | ||
|
||
// Estimate eigenvalue of U corresponding eigenvector |010⟩ | ||
let eigenvalue = EstimateEigenvalue(U, state, 6); | ||
|
||
// Reset the state qubits | ||
ResetAll(state); | ||
|
||
// The eigenvalue represented as ComplexPolar should be (1.0, π/3.0) | ||
// or approximately (1.0, 1.04719755). | ||
eigenvalue | ||
} | ||
|
||
/// # Summary | ||
/// Estimate the eigenvalue of a unitary operation `U` with certain `precision` | ||
/// for a given eigenvector using Quantum Phase Estimation (QPE) algorithm. | ||
operation EstimateEigenvalue( | ||
U : Qubit[] => Unit is Adj + Ctl, | ||
eigenstate : Qubit[], | ||
precision : Int | ||
) : ComplexPolar { | ||
// Allocate qubits to be used in phase estimation | ||
use phase = Qubit[precision]; | ||
|
||
// Estimate eigenvalue of Rz gate corresponding eigenvector |1⟩ | ||
ApplyQPE(ApplyOperationPowerCA(_, U, _), eigenstate, phase); | ||
|
||
// Measure qubit register `phase` as a binary fraction | ||
let phaseEstimation = MeasureBinaryFractionLE(phase); | ||
|
||
// Reset the qubits | ||
ResetAll(phase); | ||
|
||
// Return one data point for histogram display | ||
new ComplexPolar { | ||
Magnitude = 1.0, | ||
Argument = phaseEstimation * 2.0 * Std.Math.PI() | ||
} | ||
} | ||
|
||
/// # Summary | ||
/// Given a qubit register `qs` measure each qubit in the register | ||
/// assume reults represent a binary fraction in little-endian order | ||
/// and return the fraction as a `Double` value. | ||
operation MeasureBinaryFractionLE(qs : Qubit[]) : Double { | ||
mutable result = 0.0; | ||
mutable power = 1.0; | ||
for i in Length(qs)-1..-1..0 { | ||
power /= 2.0; | ||
if (MResetZ(qs[i]) == One) { | ||
result += power; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
// U is the unitary gate for which we want to perform phase estimation. | ||
// This operation operates on three qubits and can be represented as an 8x8 matrix. | ||
// As number of qubits gets larger, the matrix representation becomes impractical, | ||
// and finding exact eigenvalues classicaly becomes computationally prohibitive. | ||
// But representation as a quantum circuit can still be concise and efficient. | ||
operation U(qs : Qubit[]) : Unit is Ctl + Adj { | ||
// Using the Rzz gate with the angle of π/3 | ||
// The eigenvalue of this gate is exp(i * π/3) for |010⟩ | ||
Rzz(Std.Math.PI() / 3.0, qs[0], qs[1]); | ||
Rzz(Std.Math.PI() / 3.0, qs[1], qs[2]); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.