View on GitHub
Open this notebook in GitHub to run it yourself
NOTE: Quantum Fourier Transform
NOTE: Quantum Fourier Transform
The Quantum Fourier Transform (QFT) function is the quantum analog for the discrete Fourier transform. It is applied on the quantum register state vector in the following manner:Where and are the binary numbers the qubits represent. more information
Guided Implementation
To implement the Hadamard test for the QFT unitary and the state , one control qubit and an array of four target qubits are initialized. The control qubit variable will be namedexpectation_value and is of QBit type, and the target qubit array will be captured by the QArray type variable psi.
All qubits are initialized in states, and the state of the psi qubit array will remain unchanged throughout the implementation ().
Our implementation of the Hadamard test involves three main steps, followed by a measurement of the expectation_value qubit and a post-processing step to obtain the real part of the expectation value:
-
Applying the Hadamard gate to the
expectation_valuequbit as a preparation step, creating a uniform superposition. -
Applying the unitary gate on the
psiqubit array in a controlled manner, conditioned on the control qubit being in the state. - Re-application of a Hadamard gate to the control qubit that can be seen as an inverse preparation step, with acting as its own inverse.
-
A projective measurement of the
expectation_valuequbit, yielding the probabilities of measuring it in the and states.
controlled_qft that implements the controlled operation of the unitary on psi, conditioned on the control qubit expectation_value is in the state.
This is achieved by leveraging the Classiq built-in control and qft functions:
preparation_and_application, seamlessly implementing the three main steps of the Hadamard test as outlined above, using the Classiq Within-Apply statement (read more).
The Within-Apply statement performs the operation , specifically designed for situations where a preparation step is performed solely to enable the operation of a particular function and is subsequently inverted.
The preparation and inverse-preparation actions should be specified within the Within section, while the primary function operating should be specified in the Apply section.
Since the third step, which involves the re-application of the Hadamard gate to the expectation_value qubit can be regarded as an inverse-preparation step (due to being its own inverse), the Hadamard test becomes a natural candidate for the Within-Apply statement.
The preparation stage, which involves applying a Hadamard gate to the expectation_value qubit, and the re-application of the Hadamard gate to the same qubit after the controlled QFT operation on the psi qubit array are both managed within the Within section.
The function controlled_qft, which handles the controlled operation of on the psi qubit array, is specified in the Apply section:
main function that encapsulates all essential components of the algorithm. It begins with the declaration and initialization of all qubits, followed by a call to the preparation_and_application function, which implements the three core steps:
Output:
psi as a local variable in the main function (in contrast to expectation_value, which is declared globally), the execution of the quantum program on the Classiq simulator will yield the measurement outcomes of the expectation_value qubit in states and , along with the corresponding measurement probabilities.
The probability can then be algebraically manipulated to calculate the real part of the expectation value, which should align with the analytical result:
You can refer to the note below for the complete derivation.
Complete derivation
Complete derivation
After the execution of the
main function, the system is evolved into its final quantum state:Since applying QFT on is equivalent to applying a 4-qubit Hadamard transform, transforming it to the state.Running the program on the Classiq simulator outputs the measurement results for both states of the control qubits, which can be analytically calculated and compared:where the result of the inner product is used.The probabilities can then be manipulated to calculate the expectation value as , yielding the same result as the direct calculation provided above in the main text.Num shots to “100000”:
Or through the SDK, by running the following code:
Output:
Output:
tot_num_shots) determines the precision of this estimate.
Optional Exercise
Optional Exercise
Run the above example of the Hadamard Test for the QFT and the state from the SDK using 1,000, 2,000, 4,000, 8,000 and 16,000 shots.For each job, calculate the real part of the expectation value using the formula .Plot a graph of the expectation value as a function of the number of shots.Add to the graph the theoretical value.Explain the results.What is the mean and the variance for each execution? Why is that?
NOTE: Precision of the Results (read only after completing the exercise)
NOTE: Precision of the Results (read only after completing the exercise)
Increasing the
tot_num_shots parameter in the execution preferences will enhance the precision of the expectation value estimation.This improvement arises from the statistical nature of the measurements; each measurement represents a sample from a distribution modeled by a classical random variable, with the expectation value corresponding to the mean of this distribution. As a result, the law of large numbers applies, and the standard error of the sample mean is inversely proportional to the square root of the sample size:Where is the standard deviation of the random variable modeling the measurements, and represents the total number of measurements (tot_num_shots).This formula demonstrates that increasing the number of measurements reduces the statistical error of the estimated mean, resulting in a more reliable estimation of