Cost Functions
During this lesson, we'll learn how to evaluate a cost function:
- First, we'll learn about Qiskit Runtime primitives
- Define a cost function . This is a problem-specific function that defines the problem's goal for the optimizer to minimize (or maximize)
- Defining a measurement strategy with the Qiskit Runtime primitives to optimize speed vs accuracy
Primitives
All physical systems, whether classical or quantum, can exist in different states. For example, a car on a road can have a certain mass, position, speed, or acceleration that characterize its state. Similarly, quantum systems can also have different configurations or states, but they differ from classical systems in how we deal with measurements and state evolution. This leads to unique properties such as superposition and entanglement that are exclusive to quantum mechanics. Just like we can describe a car's state using physical properties like speed or acceleration, we can also describe the state of a quantum system using observables, which are mathematical objects.
In quantum mechanics, states are represented by normalized complex column vectors, or kets (), and observables are hermitian linear operators () that act on the kets. An eigenvector () of an observable is known as an eigenstate. Measuring an observable for one of its eigenstates () will give us the corresponding eigenvalue () as readout.
If you're wondering how to measure a quantum system and what you can measure, Qiskit offers two that can help:
Sampler : Given a quantum state , this primitive obtains the probability of each possible computational basis state.Estimator : Given a quantum observable and a state , this primitive computes the expected value of .
The Sampler primitive
The
Where is the number of qubits, and the integer representation of any possible output binary string (i.e. integers base ).
Qiskit Runtime's
However, since the number of possible outputs grows exponentially with the number of qubits (i.e. ), the number of shots will need to grow exponentially as well in order to capture a dense probability distribution. Therefore,
The
The Estimator primitive
The
However, calculating the expectation value of an observable is not always possible, as we often don't know its eigenbasis. Qiskit Runtime's
In simpler terms,
Any operator can be expressed as a combination of Pauli operators.
such that
where is the number of qubits, for (i.e. integers base ), and .
After performing this decomposition,
For each , the
Since calculating the expectation value of Paulis is impractical (i.e. exponentially growing),
The reader may notice the implicit assumption that probability also needs to be efficient as explained for
Guided example to calculate expectation values
Let's assume the single-qubit state , and observable
with the following theoretical expectation value
Since we do not know how to measure this observable, we cannot compute its expectation value directly, and we need to re-express it as . Which can be shown to evaluate to the same result by virtue of noting that , and .
Let see how to compute and directly. Since and do not commute (i.e. don't share the same eigenbasis), they cannot be measured simultaneously, therefore we need the auxiliary circuits:
Output:
Output:
Output:
We can now carry out the computation manually using
Output:
Sampler results:
>> Expected value of X: 1.00000
>> Expected value of Z: -0.00500
>> Total expected value: 2.00500
Estimator results:
>> Expected value of X: 1.00000
>> Expected value of Z: 0.00000
>> Total expected value: 2.00000
Mathematical rigor (optional)
Expressing with respect to the basis of eigenstates of , , it follows:
Since we do not know the eigenvalues or eigenstates of the target observable , first we need to consider its diagonalization. Given that is , there exists a unitary transformation such that where is the diagonal eigenvalue matrix, so if , and .
This implies that the expected value can be rewritten as:
Given that if a system is in the state the probability of measuring is , the above expected value can be expressed as:
It is very important to note that the probabilities are taken from the state instead of . This is why the matrix is absolutely necessary.
You might be wondering how to obtain the matrix and the eigenvalues . If you already had the eigenvalues, then there would be no need to use a quantum computer since the goal of variational algorithms is to find these eigenvalues of .
Fortunately, there is a way around that: any matrix can be written as a linear combination of tensor products of Pauli matrices and identities, all of which are both hermitian and unitary with known and . This is what Runtime's
Here are the Operators that can be used:
So let's rewrite with respect to the Paulis and identities:
where for (i.e. base ), and :
where and , such that:
Cost functions
In general, cost functions are used to describe the goal of a problem and how well a trial state is performing with respect to that goal. This definition can be applied to various examples in chemistry, machine learning, finance, optimization, and so on.
Let's consider a simple example of finding the ground state of a system. Our objective is to minimize the expectation value of the observable representing energy (Hamiltonian ):
We can use the
Notice how we will only be able to minimize the cost function for the limited set of states that we are considering. This leads us to two separate possibilities:
- Our ansatz does not define the solution state across the search space: If this is the case, our optimizer will never find the solution, and we need to experiment with other ansatzes that might be able to represent our search space more accurately.
- Our optimizer is unable to find this valid solution: Optimization can be globally defined and locally defined. We'll explore what this means in the later section.
All in all, we will be performing a classical optimization loop but relying on the evaluation of the cost function to a quantum computer. From this perspective, one could think of the optimization as a purely classical endeavor where we call some each time the optimizer needs to evaluate the cost function.
No output produced
Output:
We will first carry this out using a simulator: the StatevectorEstimator. This is usually advisable for debugging, but we will immediately follow the debugging run with a calculation on real quantum hardware. Increasingly, problems of interest are no longer classically simulable without state-of-the-art supercomputing facilities.
Output:
[1.94770104]
We will now proceed with running on a real quantum computer. Note the syntax changes. The steps involving the pass_manager will be discussed further in the next example. One step of particular importance in variational algorithms is the use of a Qiskit Runtime session. Starting a session allows you to run multiple iterations of a variational algorithm without waiting in a new queue each time parameters are updated. This is important if queue times are long and/or many iterations are needed. Only partners in the IBM Quantum Network can use Runtime sessions. If you do not have access to sessions, you can reduce the number of iterations you submit at a given time, and save the most recent parameters for use in future runs. If you submit too many iterations or encounter queue times that are too long, you may encounter error code 1217, which refers to long delays between job submissions..
Output:
[1.86750273]
Note that the values obtained from the two calculations above are very similar. Techniques for improving results will be discussed further below.
Example mapping to non-physical systems
The maximum cut (Max-Cut) problem is a combinatorial optimization problem that involves dividing the vertices of a graph into two disjoint sets such that the number of edges between the two sets is maximized. More formally, given an undirected graph , where is the set of vertices and is the set of edges, the Max-Cut problem asks to partition the vertices into two disjoint subsets, and , such that the number of edges with one endpoint in and the other in is maximized.
We can apply Max-Cut to solve a various problems including: clustering, network design, phase transitions, etc. We'll start by creating a problem graph:
Output:
This problem can be expressed as a binary optimization problem. For each node , where is the number of nodes of the graph (in this case ), we will consider the binary variable . This variable will have the value if node is one of the groups that we'll label and if it's in the other group, that we'll label as . We will also denote as (element of the adjacency matrix ) the weight of the edge that goes from node to node . Because the graph is undirected, . Then we can formulate our problem as maximizing the following cost function:
To solve this problem with a quantum computer, we are going to express the cost function as the expected value of an observable. However, the observables that Qiskit admits natively consist of Pauli operators, that have eigenvalues and instead of and . That's why we are going to make the following change of variable:
Where . We can use the adjacency matrix to comfortably access the weights of all the edge. This will be used to obtain our cost function:
This implies that:
So the new cost function we want to maximize is:
Moreover, the natural tendency of a quantum computer is to find minima (usually the lowest energy) instead of maxima so instead of maximizing we are going to minimize:
Now that we have a cost function to minimize whose variables can have the values and , we can make the following analogy with the Pauli :
In other words, the variable will be equivalent to a gate acting on qubit . Moreover:
Then the observable we are going to consider is:
to which we will have to add the independent term afterwards:
The operator is a linear combination of terms with Z operators on nodes connected by an edge (recall that the 0th qubit is farthest right): . Once the operator is constructed, the ansatz for the QAOA algorithm can easily be built by using the
Output:
Output:
Offset: -2.5
With the Runtime Estimator directly taking a Hamiltonian and parameterized ansatz, and returning the necessary energy, The cost function for a QAOA instance is quite simple:
No output produced
No output produced
Output:
0.13310009015917973
We will revisit this example in Applications to explore how to leverage an optimizer to iterate through the search space. Generally speaking, this includes:
- Leveraging an optimizer to find optimal parameters
- Binding optimal parameters to the ansatz to find the eigenvalues
- Translating the eigenvalues to our problem definition
Measurement strategy: speed versus accuracy
As mentioned, we are using a noisy quantum computer as a black-box oracle, where noise can make the retrieved values non-deterministic, leading to random fluctuations which, in turn, will harm — or even completely prevent — convergence of certain optimizers to a proposed solution. This is a general problem that we must address as we incrementally explore quantum utility and progress towards quantum advantage:
We can use Qiskit Runtime Primitive's error suppression and error mitigation options to address noise and maximize the utility of today's quantum computers.
Error Suppression
Error suppression refers to techniques used to optimize and transform a circuit during compilation in order to minimize errors. This is a basic error handling technique that usually results in some classical pre-processing to the overall runtime. The overhead includes transpiling circuits to run on quantum hardware by:
- Expressing the circuit using the native gates available on a quantum system
- Mapping the virtual qubits to physical qubits
- Adding SWAPs based on connectivity requirements
- Optimizing 1Q and 2Q gates
- Adding dynamical decoupling to idle qubits to prevent the effects of decoherence.
Primitives allow for the use of error suppression techniques by setting the
We will visualize the value of increasing optimization in the transpilation process by looking at an example circuit with a simple ideal behavior.
Output:
The circuit above can yield sinusoidal expectation values of the observable given, provided we insert phases spanning an appropriate interval, such as .
No output produced
We can use a simulator to show the usefulness of an optimized transpilation. We will return below to using real hardware to demonstrate the usefulness of error mitigation. We will use QiskitRuntimeService to get a real backend (in this case, ibm_sherbrooke), and use AerSimulator to simulate that backend, including its noise behavior.
No output produced
We can now use a pass manager to transpile the circuit into the "instruction set architecture" or ISA of the backend. This is a new requirement in Qiskit Runtime: all circuits submitted to a backend must conform to the constraints of the backend’s target, meaning they must be written in terms of the backend's ISA — i.e., the set of instructions the device can understand and execute. These target constraints are defined by factors like the device’s native basis gates, its qubit connectivity, and - when relevant - its pulse and other instruction timing specifications.
Note that in the present case, we will do this twice: once with optimization_level = 0, and once with it set to 3. Each time we will use the Estimator primitive to estimate the expectation values of the observable at different values of phase.
No output produced
Finally, we can plot the results, and we see that the precision of the calculation was fairly good even without optimization, but it definitely improved by increasing optimization to level 3. Note that in deeper, more complicated circuits, the difference between optimization levels of 0 and 3 are likely to be more significant. This is a very simple circuit used as a toy model.
Output:
Error Mitigation
Error mitigation refers to techniques that allow users to reduce circuit errors by modeling the device noise at the time of execution. Typically, this results in quantum pre-processing overhead related to model training and classical post-processing overhead to mitigate errors in the raw results by using the generated model.
The Qiskit Runtime primitive's
When implementing any error mitigation technique, we expect the in our results to be reduced with respect to the previous, unmitigated bias. In some cases, the bias may even disappear. However, this comes at a cost. As we reduce the bias in our estimated quantities, the statistical variability will increase (that is, variance), which we can account for by further increasing the number of shots per circuit in our sampling process. This will introduce overhead beyond that needed to reduce the bias, so it is not done by default. We can easily opt-in to this behavior by adjusting the number of shots per circuit in options.executions.shots, as shown in the example below.
For this course, we will explore these error mitigation models at a high level to illustrate the error mitigation that Qiskit Runtime primitives can perform without requiring full implementation details.
Twirled readout error extinction (T-REx)
Twirled readout error extinction (T-REx) uses a technique known as Pauli twirling to reduce the noise introduced during the process of quantum measurement. This technique assumes no specific form of noise, which makes it very general and effective.
Overall workflow:
- Acquire data for the zero state with randomized bit flips (Pauli X before measurement)
- Acquire data for the desired (noisy) state with randomized bit flips (Pauli X before measurement)
- Compute the special function for each data set, and divide.
We can set this with
Zero noise extrapolation
Zero noise extrapolation (ZNE) works by first amplifying the noise in the circuit that is preparing the desired quantum state, obtaining measurements for several different levels of noise, and using those measurements to infer the noiseless result.
Overall workflow:
- Amplify circuit noise for several noise factors
- Run every noise amplified circuit
- Extrapolate back to the zero noise limit
We can set this with
Each method comes with its own associated overhead: a trade-off between the number of quantum computations needed (time) and the accuracy of our results:
Using Qiskit Runtime's mitigation and suppression options
Here's how to calculate an expectation value while using error mitigation and suppression in Qiskit Runtime. We can make use of precisely the same circuit and observable as before, but this time keeping the optimization level fixed at level 2, and now tuning the resilience or the error mitigation technique(s) being used. This error mitigation process occurs multiple times throughout an optimization loop.
We perform this part on real hardware, since error mitigation is not available on simulators.
No output produced
As before, we can plot the resulting expectation values as a function of phase angle for the three levels of error mitigation used. With great difficulty, one can see that error mitigation improves the results slightly. Again, this effect is much more pronounced in deeper, more complicated circuits.
Output:
Summary
With this lesson, you learned how to create a cost function:
- Create a cost function
- How to leverage Qiskit Runtime primitives to mitigate and suppression noise
- How to define a measurement strategy to optimize speed vs accuracy
Here's our high-level variational workload:
Our cost function runs during every iteration of the optimization loop. The next lesson will explore how the classical optimizer uses our cost function evaluation to select new parameters.
Output:
1.1.0
0.23.0
Was this page helpful?