Quantum Measurement
Overview
In the design of quantum circuits, we finally need to obtain the results through the measurement operation. When measuring, we need to select a specific ground state for measurement, the measured results are uncertain, and the quantum state will also randomly collapse to at some ground state we measure.
Quantum measurement is described by a set of measurement operators
The state collapse of the system after measurement is:
The measurement operator satisfies the completeness equation:
The completeness equation expresses the fact that the probabilities sum to 1:
This equation holds for all
According to the different measurement operators selected, our common measurements are divided into computational basis measurement, projection measurement, Pauli measurement, etc. MindSpore Quantum provides a wealth of measurement functions and visual display tools, and we use these functions to further learn quantum measurement.
Computational Basis Measurement
We first have a simple understanding of the calculation basis measurement: suppose there is a state of n qubits, we perform n-bit calculation basis measurement on it. After the measurement, if the result is
Single-qubit Measurement Under Computational Basis
Computational base measurement operators:
Assuming that the measured state
Similarly, the probability of obtaining measurement 1 is
Measurement of Multiple Qubits Under the Computational Base - Taking Two Qubits as An Example
Measure All Bits in the System
Computational basis measurement operator in two-qubit system:
Assuming that the measured state
Similarly, the probability of obtaining measurement 01 is
Measuring a Single Qubit in the System
Measuring the first qubit of a two-qubit quantum state, the two-computational basis measurement operator:
Assuming that the measured state
Similarly, the probability of getting measurement 1 is
Through the study of computation-based measurements, we can intuitively realize that making a measurement on one of the bits of a multi-qubit state essentially projects the quantum state into one of the two subspaces. To distinguish these two subspaces concisely, we use our knowledge of linear algebra to know that two orthogonal subspaces can be described by matrices with exactly two unique eigenvalues.
MindSpore Quantum Implementation for Computational Base Measurements
Next, we use MindSpore Quantum to build a quantum circuit with measurement operations and observe the results. First, import the modules that this tutorial depends on.
[1]:
import numpy as np # Import the numpy library and abbreviate to np
from mindquantum.core.gates import X, H # Import quantum gate H, X
from mindquantum.simulator import Simulator # Import the Simulator class from mindquantum. simulator
from mindquantum.core.circuit import Circuit # Import the Circuit module for building quantum circuits
from mindquantum.core.gates import Measure # Import measurement gate
Description:
Numpy is a powerful Python library, mainly used to perform calculations on multi-dimensional arrays, supports a large number of dimensional array and matrix operations, and also provides a large number of mathematical function libraries for array operations;
MindSpore Quantum is a quantum-classical hybrid computing framework that supports the training and reasoning of various quantum neural networks;
The quantum gate to be executed in the built quantum circuit needs to be imported from the mindquantum.core module;
The quantum simulator required to run the quantum circuit needs to be imported from the mindquantum.simulator module;
The quantum circuit class Circuit required to build a quantum circuit needs to be imported from the mindquantum.core module;
To measure the quantum circuit, we need to import the measure operation from MindSpore Quantum.
We build a quantum circuit that prepares a two-qubit uniform superposition state
MindSpore Quantum Implements Measuring All Qubits in the System
Before using the code demonstration, let’s simply calculate the theoretical value.
Use the computational basis to measure
It can be seen that there are only two possibilities for the measurement result: 00 and 11, and the probability is
We start building a quantum circuit that prepares
[2]:
circ_all = Circuit() # Initialize the quantum circuit
circ_all += H.on(0) # The H gate acts on qubit 0
circ_all += X.on(1, 0) # The X gate acts on qubit 1 and is controlled by qubit 0
circ_all += Measure('q0').on(0) # Apply a measurement on qubit 0 and name this measurement 'q0'
circ_all += Measure('q1').on(1) # Apply a measurement to qubit 1 and name this measurement 'q1'
circ_all.svg() # Draw a quantum circuit picture in SVG format
[2]:
[3]:
sim = Simulator('mqvector', 2) #Declare a 2-bit mqvector emulator
sim.apply_circuit(circ_all).svg() # Run the quantum circuit on the simulator
[3]:
It can be seen that the measurement result we get is ‘11’ (this result can be ‘00’, since the measurement is random), and the quantum state after measurement collapses to:
[4]:
print(sim.get_qs(True))
1¦00⟩
The quantum state collapses to
If we measure a few more times, we can find that the measurement result will also be ‘00’, please execute multiple times to see different result:
[5]:
sim.reset() # Reset simulator
sim.apply_circuit(circ_all).svg() # Run the quantum circuit on the simulator
[5]:
Printing out the quantum state at this time, we can see that it collapses into the corresponding
[6]:
print(sim.get_qs(True))
1¦00⟩
We observe that the results are sometimes ‘00’ and sometimes ‘11’, which is in line with the theoretical expectation, but there is no way to observe whether the probabilities of 00 and 11 are the same. We hope to make multiple measurements and count the frequency of different results, so as to see whether the results meet the expected probability distribution. For this purpose, we use the quantum line Sampling function:
[7]:
sim.reset()
result = sim.sampling(circ_all, shots=1000) # Sample the circuit defined above 1000 times
result.svg()
[7]:
We can see that in the sampling 1000, ‘00’ appears 476 times, and ‘11’ appears 524 times (those value may change when you executer the cell). The sampling result conforms to the probability distribution, and the slight error is caused by the noise of the simulator. Students who read it carefully can find that in the Quantum Simulator Tutorial, we have shown the sampling results of this circuit, but the reason for the distribution of the results is not explained. After learning the computation based measurement in this tutorial, we believe that you have a deeper understanding of the distribution of the results.
MindSpore Quantum Implements Measurement of a Single Bit in the System
Again, let’s simply calculate the theoretical value before using the code to demonstrate it.
Using the computational basis to measure
It can be seen that there are two possibilities for the measurement result: 0 and 1, and the probability is
We start to build a quantum circuit that prepares
[8]:
circ_partial = Circuit() # Initialize the quantum circuit
circ_partial += H.on(0) # The H gate acts on qubit 0
circ_partial += X.on(1, 0) # The X gate acts on qubit 1 and is controlled by qubit 0
circ_partial += Measure('q0').on(0) # Apply a measurement on qubit 0 and name this measurement 'q0'
circ_partial.svg() # Draw a quantum circuit picture in SVG format
[8]:
[9]:
sim.reset() # Reset simulator
sim.apply_circuit(circ_partial).svg() # Run the quantum circuit on the simulator
[9]:
It can be seen that the measurement result we get is ‘1’ (this can also be ‘0’ when you execute the cell), and the quantum state after measurement collapses to:
[10]:
print(sim.get_qs(True))
1¦11⟩
The quantum state collapses to
Similarly, if we measure a few more times, we can find that the measurement result will also be ‘0’, which will not be demonstrated here. We directly sample 1000 observations of this quantum circuit:
[11]:
sim.reset()
result = sim.sampling(circ_partial, shots=1000) #Sample the circuit defined above 1000 times
result.svg()
[11]:
We can see that in sampling 1000, ‘0’ appears 483 times and ‘1’ appears 517 times (those values may change when you execute the cell). The sampling result conforms to the probability distribution, and the slight error is caused by the noise of the simulator.
We have completed the study of quantum computational based measurement, and then we enter the study of another measurement operation: projection measurement.
Projection Measurement
Projection measurement is described by an observable Hermite operator
Here
The state of the quantum system immediately after the measurement is:
The intuitive explanation is that we use the
An important feature of projection measurement is that it is easy to calculate the expected value of projection measurement
Projection measurement can be regarded as a special case of general measurement. When the measurement operator not only satisfies the completeness relation
With these additional constraints, general measurement degenerates into projection measurement.
Pauli Measurement
Finally, we learn the Pauli measurement. Pauli measurement is a projection measurement that selects the observable
It can be seen that Z satisfies
Using Z to do projection measurement, if the measurement result is +1, we can conclude that the state of the qubit is projected into the +1 eigensubspace
MindSpore Quantum provides us with the ability to calculate the expected value of a projected measurement based on a given observable H:
get_expectation(hamiltonian) can calculate the expected value of the current quantum state of the simulator with respect to an observed quantity:
For example, we wish to apply a Pauli-Z measurement to the q1 qubit on a system in the state
[12]:
sim = Simulator('mqvector', 2) # Declare a 2-bit mqvector simulator
sim.set_qs(np.array([2**0.5 / 2, 0, 0, 2**0.5 / 2])) # Setting simulator State
print(sim.get_qs())
[0.70710678+0.j 0. +0.j 0. +0.j 0.70710678+0.j]
Then we construct the Hamiltonian hams corresponding to Pauli-Z measurement on q1:
[13]:
from mindquantum.core.operators import Hamiltonian # Import Hamiltonian definition module
from mindquantum.core.operators import QubitOperator # Import sparse operator definition module
hams = Hamiltonian(QubitOperator('Z1')) # Construct the Hamiltonian for Pauli-Z measurement on q1
In order to deeply understand and learn the Pauli-Z measurement operation, we first manually calculate the expected value of the current quantum state of the simulator to perform the Pauli-Z measurement on q1, and calculate the probability that the measurement result is +1, -1:
This means that the theoretical expectation value of the measurement is 0, and the probability of measuring +1 and -1 is 50%. We use the get_expectation() provided by MindSpore Quantum to verify the result:
[14]:
# Calculate the expected value of the current quantum state of the simulator with respect to Hams
sim.get_expectation(hams)
[14]:
0j
As you can see, the result of the manual calculation and the calculation using get_expectation(hamiltonian) is the same, as expected.
We can also act on both the q0 and q1 qubits on the system in the
[15]:
# Construct the Hamiltonian for Pauli-Z measurements on q0, q1
hams2 = Hamiltonian(QubitOperator('Z0') + QubitOperator('Z1'))
We can also manually calculate the expected value of the Pauli-Z measurement of the current quantum state of the simulator on q0, q1:
[16]:
sim.set_qs(np.array([2**0.5 / 2, 0, 0, 2**0.5 / 2])) # Set simulator state
# Calculate the expected value of the simulator current quantum state about hams2
sim.get_expectation(hams2)
[16]:
0j
This operation does not change the quantum state, we look at the current quantum state:
[17]:
sim.get_qs()
[17]:
array([0.70710678+0.j, 0. +0.j, 0. +0.j, 0.70710678+0.j])
It can be found that the quantum state is still the original set
We learned to recognize an important operation in quantum computing - measurement, used MindSpore Quantum to measure quantum circuits to verify our theoretical results, and used different visualization tools to display the measurement results.
To learn higher-level operations on quantum circuits in MindSpore Quantum, build and train quantum-classical hybrid neural networks, see the documentation for get_expectation_with_grad() and apply_hamiltonian().
[18]:
from mindquantum.utils.show_info import InfoTable
InfoTable('mindquantum', 'scipy', 'numpy')
[18]:
Software | Version |
---|---|
mindquantum | 0.9.11 |
scipy | 1.10.1 |
numpy | 1.23.5 |
System | Info |
Python | 3.9.16 |
OS | Linux x86_64 |
Memory | 8.3 GB |
CPU Max Thread | 8 |
Date | Sat Dec 30 23:26:48 2023 |