page_type | languages | products | description | urlFragment | |||
---|---|---|---|---|---|---|---|
sample |
|
|
Iterative phase estimation calculating an inner product between two vectors. |
iterative-phase-estimation |
This sample code and notebook was written by members of KPMG Quantum team in Australia. It aims to demonstrate expanded capabilities of Basic Measurement Feedback targets and makes use of bounded loops, classical function calls at run time, nested conditional if statements, mid circuit measurements and qubit reuse.
- iterative-phase-estimation.qs: Q# sample code.
- iterative-phase-estimation.ipynb: Jupyter notebook.
- iterative-phase-estimation.csproj: CS Project file.
This notebook demonstrates an iterative phase estimation within Q#. It will use iterative phase estimation to calculate an inner product between two 2-dimensional vectors encoded on a target qubit and an ancilla qubit. An additional control qubit is also initialised which will be the only qubit used for measurement.
The circuit begins by encoding the pair of vectors on the target qubit and the ancilla qubit. It then applies an Oracle operator to the entire register, controlled off the control qubit (which is set up in the
The vectors v and c are to be encoded onto the target qubit and the ancilla qubit. The vector
A Y rotation applied to a target qubit in the
Note: A factor of 2 is present here on theta. An application of a
The register of the target qubit and ancilla qubit is,
which also takes the form,
An oracle G needs to be constructed such that it generates an eigenphase on the state encoded on the target qubit and the ancilla qubit. The construction of this oracle is unimportant to the demonstration within this notebook, but the operation it applies is,
where the inner product
Now the control qubit contains the phase
Now for the iterative part of the circuit. For n measurements, consider that the phase can be represented as a binary value
where
Applying
Consider that the phase has no terms deeper than
Now the value
Using the previous measured value for
This process is iteratively applied for some bit precision n to obtain the phase
As the readout tells nothing of either vector, only the inner product between them, the states on the target qubit and ancilla qubit remain in the same state throughout the process!
Finally to calculate the inner product from the measured value,
where
Note: For inner product that are not -1 or 1, the solutions are paired with a value difference of
Note: For inner product solutions between the discrete bit precision, a distribution of results will be produced based on where the inner product lies between the discrete bit value.
It is suggested that the SimulateInnerProduct is run first by placing "@EntryPoint()" before the operation SimulateInnerProduct and by using:
dotnet run
in the terminal before running on an Azure target. This version of the inner product operation will output additional information. This includes the manipulation of doubles of which the output is displayed in the terminal.
When running the job via the terminal using VS Code the following call should be made:
az quantum job submit --target-id quantinuum.sim.h1-1e --target-capability AdaptiveExecution --shots 128 --job-name IterativePhaseEstimation
Note: The target requires a target execution profile that supports basic measurement feedback.
This will submit a job with the name "IterativePhaseEstimation". The circuit is approximately 0.4 EHQC each shot. The number of shots specified is 128, but this can be increased to reduce the variance of the result, up to some stable distribution. The total EHQCs for a job can be viewed within the Azure portal under "Job management". Selecting the desired job, the cost estimation can be viewed. It is not suggested to increase the number of measurements beyond 3 for running on Azure targets as the EHQCs can increase significantly. Be sure to place "@EntryPoint()" before the operation HardwareInnerProduct, not SimulateInnerProduct as this operation contains calls such as "Message" which is only used for printing to the terminal within VS Code. Job data can be accessed as normal through the Azure portal or via the terminal using
az quantum job output -j JOB_ID -o table
replacing "JOB_ID" with the job id. The results show a solution in the state with the majority population. The final inner product from the integer results can be calculated by
Note: Choosing input parameters which only has one solution state (inner produces of -1 or 1) are ideal for visibility at a low number of shots.