Lesson 07: Quantum simulation
Yukio Kawashima (May 30, 2024)
Approximate QPU time to run this experiment is 7 seconds.
This notebook is mostly taken from a tutorial notebook for Qiskit Algorithms.
Click here to download this notebook in Jupyter format.
Click here to download the pdf of the original lecture. Please note that some code snippets may become deprecated since these are static images.
1. Introduction
As a real time evolution technique, Trotterization consists in the successive application of a quantum gate or gates, chosen to approximate the time evolution of a system for a time slice. Following from the Schrödinger equation, the time evolution of a system initially in the state takes the form:
where is the time-independent Hamiltonian governing the system. We consider a Hamiltonian that can be written as a weighted sum of Pauli terms , with representing a tensor product of Pauli terms acting on qubits. In particular, these Pauli terms might commute with one another, or they might not. Given a state at time , how do we obtain the system's state at a later time using a quantum computer? The exponential of an operator can be most easily understood through its Taylor series:
Some very basic exponentials, like can be implemented easily on quantum computers using a compact set of quantum gates. Most Hamiltonians of interest will not have just a single term, but will instead have many terms. Note what happens if :
When and commute, we have the familiar case (which is also true for numbers, and variables and below):
But when operators do not commute, terms cannot be rearranged in the Taylor series to simplify in this way. Thus, expressing complicated Hamiltonians in quantum gates is a challenge.
One solution is to consider very small time , such that the first-order term in the Taylor expansion dominates. Under that assumption:
Of course, we may need to evolve our state for a longer time. That is accomplished by using many such small steps in time. This process is called Trotterization:
Here is the time slice (evolution step) that we are choosing. As a result, a gate to be applied times is created. A smaller timestep leads to a more accurate approximation. However, this also leads to deeper circuits which, in practice, leads to more error accumulation (a non-negligible concern on near-term quantum devices).
Today, we will study the time evolution of the Ising model on linear lattices of and sites. These lattices consist of an array of spins that interact only with their nearest neighbors. These spins can have two orientations: and , which correspond to a magnetization of and respectively.
where describes the interaction energy, and the magnitude of an external field (in the x-direction above, but we will modify this). Let us write this expression using Pauli matrices, and considering that the external field has an angle with respect to the transversal direction,
This Hamiltonian is useful in that it allows us to easily study the effects of an external field. In the computational basis, the system will be encoded as follows:
Quantum state | Spin representation |
---|---|
We will start investigating the time evolution of such a quantum system. More specifically, we will visualize the time-evolution of certain properties of the system like magnetization.
1.1 Requirements
Before starting this tutorial, be sure you have the following installed:
- Qiskit SDK 1.2 or later ( pip install qiskit )
- Qiskit Runtime 0.30 or later ( pip install qiskit-ibm-runtime )
- Numpy 1.24.1 or later < 2 ( pip install numpy )
1.2 Import the libraries
Note that some libraries that may be useful (MatrixExponential, QDrift) are included even though they are not used in this current notebook. You can try them out if you have time!
Output:
'1.3.0'
No output produced
2. Mapping your problem
2.1 Defining the transverse-field Ising Hamiltonian
We here consider the 1-D transverse-field Ising model.
First, we will create a function that takes in the system parameters , , and , and returns our Hamiltonian as a
No output produced
Define the Hamiltonian
The system that we now consider has a size of , , and as an example.
Output:
SparsePauliOp(['IIIIZZ', 'IIIZZI', 'IIZZII', 'IZZIII', 'ZZIIII', 'IIIIIZ', 'IIIIZI', 'IIIZII', 'IIZIII', 'IZIIII', 'ZIIIII', 'IIIIIX', 'IIIIXI', 'IIIXII', 'IIXIII', 'IXIIII', 'XIIIII'],
coeffs=[-0.2 +0.j, -0.2 +0.j, -0.2 +0.j, -0.2 +0.j,
-0.2 +0.j, -0.45922012+0.j, -0.45922012+0.j, -0.45922012+0.j,
-0.45922012+0.j, -0.45922012+0.j, -0.45922012+0.j, -1.10865544+0.j,
-1.10865544+0.j, -1.10865544+0.j, -1.10865544+0.j, -1.10865544+0.j,
-1.10865544+0.j])
2.2 Set the parameters of the time-evolution simulation
Here we will consider three different Trotterization techniques:
- Lie–Trotter (first order)
- second-order Suzuki–Trotter
- fourth-order Suzuki–Trotter
The latter two will be used in the exercise, and in the appendix.
No output produced
2.3 Prepare the quantum circuit 1 (Initial state)
Create an initial state. Here we will start with a spin configuration of .
Output:
2.4 Prepare the quantum circuit 2 (Single circuit for time evolution)
We here construct a circuit for a single time step using Lie–Trotter.
The Lie product formula (first order) is implemented in the LieTrotter class. A first order formula consists of the approximation stated in the introduction, where the matrix exponential of a sum is approximated by a product of matrix exponentials:
As previously mentioned, very deep circuits lead to the accumulation of errors, and cause problems for modern quantum computers. Because two-qubit gates have higher error rates than single-qubit gates, a quantity of particular interest is the two-qubit circuit depth. What really matters is the two-qubit circuit depth after transpilation (since that is the circuit the quantum computer actually executes). But let us get in the habit of counting the operations for this circuit, even now using the simulator.
Output:
Trotter step with Lie-Trotter
-----------------------------
Depth: 17
Gate count: 27
Nonlocal gate count: 10
Gate breakdown: U3: 12, CX: 10, U1: 5
2.5 Set the operators to measure
Let us define a magnetization operator , and a mean spin correlation operator .
Output:
magnetization : SparsePauliOp(['IIIIIZ', 'IIIIZI', 'IIIZII', 'IIZIII', 'IZIIII', 'ZIIIII'],
coeffs=[0.16666667+0.j, 0.16666667+0.j, 0.16666667+0.j, 0.16666667+0.j,
0.16666667+0.j, 0.16666667+0.j])
correlation : SparsePauliOp(['IIIIZZ', 'IIIZZI', 'IIZZII', 'IZZIII', 'ZZIIII'],
coeffs=[0.2+0.j, 0.2+0.j, 0.2+0.j, 0.2+0.j, 0.2+0.j])
2.6 Perform time-evolution simulation
We will monitor the energy (expectation value of the Hamiltonian), magnetization (expectation value of the magnetization operator), and mean spin correlation (expectation value of the mean spin correlation operator). Qiskit's
No output produced
2.7 Plot the time evolution of the observables
We plot the expectation values we measured against time.
Output:
Text(0.5, 0.98, 'Observable evolution')
3. Exercise: Perform simulation using second-order Suzuki–Trotter
Now let's try performing simulation with second-order Suzuki–Trotter following the example of Lie–Trotter shown above.
The second-order Suzuki-Trotter can be used in Qiskit by means of the SuzukiTrotter class. Using this formula, a second order decomposition is:
3.1 Construct a circuit for a single time step
Use product_formula_st2 (SuzukiTrotter(order=2)) and construct a circuit for a single time step using second-order Suzuki–Trotter. Also, count the number of gates and depth of the circuit and compare with Lie–Trotter.
Output:
Trotter step with second-order Suzuki-Trotter
-----------------------------
Depth: 34
Gate count: 53
Nonlocal gate count: 20
Gate breakdown: U3: 23, CX: 20, U1: 10
3.2 Perform time-evolution simulation
Perform time evolution using second-order Suzuki–Trotter.
No output produced
3.3 Plot the second-order Suzuki–Trotter results
Output:
3.4 Compare with exact results
The data below is the precomputed exact results from classical computer
No output produced
Output:
4. Run on a real quantum computer
We next run the time-evolution simulation on the quantum hardware. We will work on a smaller problem, lattice size N=2. We vary the parameter and see the difference in dynamics of the wavefunction.
4.1 Step 1: Map classical inputs to a quantum problem
Pick the initial setting of the simulation:
No output produced
Then set the initial circuit:
The initial spin configuration will be "down-up"
Output:
Now compute the reference value using an ideal statevector simulator.
Output:
<matplotlib.legend.Legend at 0x7f673ca75280>
We have prepared a system initially with a sequence of spins , which corresponds to . After letting it evolve for under a transversal field (), we are almost guaranteed to measure , i.e. have a spin swap. (Note that the labels are interpreted from right to left). If the field is longitudinal (), we will have no evolution, therefore we will measure the system as it was initially prepared, . With intermediate angles, at , we will be able to measure all combinations will different probabilities, being a spin swap the most likely with a probability of 67%.
Construct circuit for HW experiment
No output produced
4.2 Step 2: Optimize for target hardware
We specify a backend.
No output produced
Then we transpile the circuit for the selected backend.
No output produced
Check out the circuit.
Output:
4.3 Step 3: Execute with Qiskit Runtime primitives
Qiskit's
Output:
job id: cw31k8xvka8g008bdxy0
Save the results
No output produced
4.4 Step 4: Post-process results
Construct the histogram of the bitstrings, which corresponds to analyzing the wavefunction, and compare them with the ideal values shown above.
Output:
<matplotlib.legend.Legend at 0x319571090>
We here show an example for constructing a circuit using higher-order (fourth-order) Suzuki–Trotter. Now let's try constructing a circuit simulation with fourth-order Suzuki–Trotter following the examples shown above.
The fourth-order Suzuki–Trotter can be used in Qiskit by means of the SuzukiTrotter class. The fourth-order can be evaluated using the following recursion relation. Note that the order of Suzuki–Trotter is denoted as "2k" in the following equations.
Construct a circuit for a single time step
Use product_formula_st4 (SuzukiTrotter(order=4)) and construct a circuit for a single time step using fourth-order Suzuki–Trotter. Also, count the number of gates and depth of the circuit and compare with Lie–Trotter and second-order Suzuki–Trotter.
Output:
Trotter step with second-order Suzuki-Trotter
-----------------------------
Depth: 170
Gate count: 265
Nonlocal gate count: 100
Gate breakdown: U3: 115, CX: 100, U1: 50
No output produced
Was this page helpful?