Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Expectation Values and Parameterized Quantum Programs

This tutorial covers the basics of measuring observables expressed as linear combinations of Pauli strings and executing a parameterized quantum program using Classiq via Python SDK. Alternatively, you can use the Classiq IDE web page to execute quantum algorithms. A parameterized quantum program is a quantum circuit with adjustable parameters, such as angles in rotation gates, that can be tuned to alter the circuit’s behavior. Think of it like tuning a camera with adjustable settings: the camera (the circuit) stays the same, but adjusting the settings (parameters) changes the captured images (outputs). In quantum computing, tuning these parameters helps identify the configuration that yields the most useful results. These programs are particularly useful in quantum machine learning and optimization, where the goal is to find the best parameter set. First, we create a parameterized quantum program using two qubits. The program applies an X gate, a parameterized RY rotation, and a CX gate. The rotation angle is controlled by a variable called angle.
Output:
The first thing we can do is to sample the outputs of the quantum program for a given parameter. For example, π/2\pi / 2. We can now execute the quantum program and obtain a sample of output states using ExecutionSession. To do this, we define the parameter values using a dictionary.
After generating the ExecutionSession, it is possible to show the counts for this particular parameter value:
Output:
Output:
Running your circuit with different parameter values helps explore how the output changes, revealing trends or minima in a cost function. This is especially useful in quantum optimization. As an example, we’ll evaluate the circuit over 50 values of angle from 00 to 2π2\pi.
Output:
When the parameters argument is a list, the result of sample is also a list of results, one for each angle value. Therefore, we can analyze the data from each parameter on angles_list. For example, an interesting way of analyzing this data is to plot the number of counts of the states 00|00\rangle and 11|11\rangle as functions of angle:
output

Measuring Pauli Strings

Measuring observables from a quantum program turns out to be necessary when you want to obtain information that can’t be accessed only from the populations of states. For this end, you can measure Pauli Strings using Classiq. As an example, if we want to measure how close the output of our system is to the Bell state: Φ+=12(00+11),|\Phi^+ \rangle = \frac{1}{\sqrt{2}} \left( |00\rangle + |11\rangle \right), it is possible measure the expected value of its projection: P(Φ+)=Φ+Φ+=12(00+11)(00+11)=12(0000+0011+1100+1111).P(\Phi^+) = |\Phi^+ \rangle \langle \Phi^+ | = \frac{1}{2} \left( |00\rangle + |11\rangle \right) \left( \langle00| + \langle 11| \right) = \frac{1}{2} \left ( |00\rangle \langle 00| + |00\rangle \langle 11 | + |11 \rangle \langle 00 | + |11\rangle \langle |11\right). The projector operator, by its turn, can be represented as a Pauli string: P(Φ+)=14(II+XXYY+ZZ)P(\Phi^+) = \frac{1}{4} \left( II + XX - YY + ZZ \right) Therefore, if we want to measure the projector expected value for some output of the quantum circuit, say angle =π/5= \pi/5, it is possible using estimate and ExecutionSession. For this, first we need to define the Hamiltonian to be measured:
Now, using observe, evaluate the expected value of the output:
Output:
Output:
The same can be done in batches, for example, if we want to plot a graph of fidelity between the output of the quantum program and the Φ+|\Phi^+\rangle state:
Output:
output

Retrieving Jobs Executed Using Execution Session

When executing a job that may take longer to complete, or when running on a hardware backend with a job queue, it is useful to have a way to retrieve the job results later. For this purpose, Classiq supports submitting an ExecutionJob, which is associated with a unique job ID. The job can then be retrieved later using this ID. To submit a job, use an ExecutionSession together with one of the execution functions that has the submit_ prefix, such as submit_sample. As an example, we submit two different jobs and then retrieve their outputs using their job IDs. First, we submit the jobs:
Once you have the job ID, it is possible to retrieve its execution data using ExecutionJob. For example, here we retrieve a previously execution of estimate_job and compare it to the outputs of first_sample - they should be close:
Output:
As expected, the retrieved job result is very close to the first sample, since they execute the same quantum circuit.

Application: Variational Quantum Circuit to Prepare a Bell State

In this additional session, we create a simple parameterized quantum algorithm that prepares the Bell State Φ+|\Phi^+\rangle. For this, an ansatz with two parameters is constructed:
Then define the function that is subject to classical optimization. In this case, we aim to maximize the expected value of the projector_operator. Therefore, we create a negative_coeffs_projector_operator to minimize:
The final step is to perform the optimization of the cost function defined by the quantum ansatz. In this tutorial, the minimize method from ExecutionSession will be employed.
Output:
Output:
These values corresponds to the quantum circuit that generates this Bell State using RX, RY, and CX gates.

Final Remarks

In this tutorial, we built a simple parameterized quantum circuit, explored sampling it with specific parameter values, and visualized how output probabilities vary with those parameters. At the end, a simple Variational Quantum Algorithm is presented to prepare a Bell State. These techniques form the foundation for building and optimizing more complex quantum algorithms.