Cost functions
Halaman ini belum diterjemahkan. Anda sedang melihat versi asli dalam bahasa Inggris.
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 versus 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 primitives 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 Sampler primitive calculates the probability of obtaining each possible state from the computational basis, given a quantum circuit that prepares the state . It calculates
Where is the number of qubits, and the integer representation of any possible output binary string (that is, integers base ).
The Qiskit Runtime Sampler runs the circuit multiple times on a quantum device, performing measurements on each run, and reconstructing the probability distribution from the recovered bitstrings. The more runs (or shots) it performs, the more accurate the results will be, but this requires more time and quantum resources.
However, since the number of possible outputs grows exponentially with the number of qubits (that is, ), the number of shots will need to grow exponentially as well in order to capture a dense probability distribution. Therefore, Sampler is only efficient for sparse probability distributions; where the target state must be expressible as a linear combination of the computational basis states, with the number of terms growing at most polynomially with the number of qubits:
The Sampler can also be configured to retrieve probabilities from a subsection of the circuit, representing a subset of the total possible states.
The Estimator primitive
The Estimator primitive calculates the expectation value of an observable for a quantum state ; where the observable probabilities can be expressed as , being the eigenstates of the observable . The expectation value is then defined as the average of all possible outcomes (that is, the eigenvalues of the observable) of a measurement of the state , weighted by the corresponding probabilities:
However, calculating the expectation value of an observable is not always possible, as we often don't know its eigenbasis. The Qiskit Runtime Estimator uses a complex algebraic process to estimate the expectation value on a real quantum device by breaking down the observable into a combination of other observables whose eigenbasis we do know.
In simpler terms, Estimator breaks down any observable that it doesn't know how to measure into simpler, measurable observables called Pauli operators.
Any operator can be expressed as a combination of Pauli operators.
such that
where is the number of qubits, for (that is, integers base ), and .
After performing this decomposition, Estimator derives a new circuit for each observable (from the original circuit), to effectively diagonalize the Pauli observable in the computational basis and measure it. We can easily measure Pauli observables because we know ahead of time, which is not the case generally for other observables.
For each , the Estimator runs the corresponding circuit on a quantum device multiple times, measures the output state in the computational basis, and calculates the probability of obtaining each possible output . It then looks for the eigenvalue of corresponding to each output , multiplies by , and adds all the results together to obtain the expected value of the observable for the given state .
Since calculating the expectation value of Paulis is impractical (that is, exponentially growing), Estimator can only be efficient when a large amount of are zero (that is, sparse Pauli decomposition instead of dense). Formally we say that, for this computation to be efficiently solvable, the number of non-zero terms has to grow at most polynomially with the number of qubits :
The reader may notice the implicit assumption that probability sampling also needs to be efficient as explained for Sampler, which means
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