Improving energy estimation of a Fermionic Hamiltonian with SQD
Estimated QPU usage: 36 seconds (tested on IBM Sherbrooke)
Background
In this tutorial, we show how to post-process noisy quantum samples to find an approximation to the ground state of the nitrogen molecule at equilibrium bond length, using the sample-based quantum diagonalization (SQD) algorithm, for samples taken from a 36-qubit quantum circuit (32 system qubits + 4 ancilla qubits). A Qiskit-based implementation is available as a Qiskit Addons, more details can be found in the corresponding docs with a simple example to get started. SQD is a technique for finding eigenvalues and eigenvectors of quantum operators, such as a quantum system Hamiltonian, using quantum and distributed classical computing together. Classical distributed computing is used to process samples obtained from a quantum processor, and to project and diagonalize a target Hamiltonian in a subspace spanned by them. This allows SQD to be robust to samples corrupted by quantum noise and deal with large Hamiltonians, such as chemistry Hamiltonians with millions of interaction terms, beyond the reach of any exact diagonalization methods. An SQD-based workflow has the following steps:
- Choose a circuit ansatz and apply it on a quantum computer to a reference state (in this case, the Hartree-Fock state).
- Sample bitstrings from the resulting quantum state.
- Run the self-consistent configuration recovery procedure on the bitstrings to obtain the approximation to the ground state.
Quantum chemistry
The properties of molecules are largely determined by the structure of the electrons within them. As fermionic particles, electrons can be described using a mathematical formalism called second quantization. The idea is that there are a number of orbitals, each of which can be either empty or occupied by a fermion. A system of orbitals is described by a set of fermionic annihilation operators that satisfy the fermionic anticommutation relations,
The adjoint is called a creation operator.
So far, our exposition has not accounted for spin, which is a fundamental property of fermions. When accounting for spin, the orbitals come in pairs called spatial orbitals. Each spatial orbital is composed of two spin orbitals, one that is labeled "spin-" and one that is labeled "spin-". We then write for the annilation operator associated with the spin-orbital with spin () in spatial orbital . If we take to be the number of spatial orbitals, then there are a total of spin-orbitals. The Hilbert space of this system is spanned by orthonormal basis vectors labeled with two-part bitstrings .
The Hamiltonian of a molecular system can be written as
where the and are complex numbers called molecular integrals that can be calculated from the specification of the molecule using a computer program. In this tutorial, we compute the integrals using the PySCF software package.
For details about how the molecular Hamiltonian is derived, consult a textbook on quantum chemistry (for example, Modern Quantum Chemistry by Szabo and Ostlund). For a high-level explanation of how quantum chemistry problems are mapped onto quantum computers, check out the lecture Mapping Problems to Qubits from Qiskit Global Summer School 2024.
Local unitary cluster Jastrow (LUCJ) ansatz
SQD requires a quantum circuit ansatz to draw samples from. In this tutorial, we'll use the local unitary cluster Jastrow (LUCJ) ansatz due to its combination of physical motivation and hardware-friendliness.
The LUCJ ansatz is a specialized form of the general unitary cluster Jastrow (UCJ) ansatz, which has the form
where is a reference state, often taken to be the Hartree-Fock state, and the and have the form
where we have defined the number operator
The operator is an orbital rotation, which can be implemented using known algorithms in linear depth and using linear connectivity. Implementing the term of the UCJ ansatz requires either all-to-all connectivity or the use of a fermionic swap network, making it challenging for noisy pre-fault-tolerant quantum processors that have limited connectivity. The idea of the local UCJ ansatz is to impose sparsity constraints on the and matrices which allow them to be implemented in constant depth on qubit topologies with limited connectivity. The constraints are specified by a list of indices indicating which matrix entries in the upper triangle are allowed to be nonzero (since the matrices are symmetric, only the upper triangle needs to be specified). These indices can be interpreted as pairs of orbitals that are allowed to interact.
As an example, consider a square lattice qubit topology. We can place the and orbitals in parallel lines on the lattice, with connections between these lines forming "rungs" of a ladder shape, like this:
With this setup, orbitals with the same spin are connected with a line topology, while orbitals with different spins are connected when they share the same spatial orbital. This yields the following index constraints on the matrices:
In other words, if the matrices are nonzero only at the specified indices in the upper triangle, then the term can be implemented on a square topology without using any swap gates, in constant depth. Of course, imposing such constraints on the ansatz makes it less expressive, so more ansatz repetitions may be required.
The IBM hardware has a heavy-hex lattice qubit topology, in which case we can adopt a "zig-zag" pattern, depicted below. In this pattern, orbitals with the same spin are mapped to qubits with a line topology (red and blue circles), and a connection between orbitals of different spin is present at every 4th spatial orbital, with the connection being facilitated by an ancilla qubit (purple circles). In this case, the index constraints are
Sample-based quantum diagonalization (SQD)
The self-consistent configuration recovery procedure is designed to extract as much signal as possible from noisy quantum samples. Because the molecular Hamiltonian conserves particle number and spin Z, it makes sense to choose a circuit ansatz that also conserves these symmetries. When applied to the Hartree-Fock state, the resulting state has a fixed particle number and spin Z in the noiseless setting. Therefore, the spin- and spin- halves of any bitstring sampled from this state should have the same Hamming weight as in the Hartree-Fock state. Due to the presence of noise in current quantum processors, some measured bitstrings will violate this property. A simple form of postselection would discard these bitstrings, but this is wasteful because those bitstrings might still contain some signal. The self-consistent recovery procedure attempts to recover some of that signal in post-processing. The procedure is iterative and requires as input an estimate of the average occupancies of each orbital in the ground state, which is first computed from the raw samples. The procedure is run in a loop, and each iteration has the following steps:
- For each bitstring that violates the specified symmetries, flip its bits with a probabilistic procedure designed to bring the bitstring closer to the current estimate of the average orbital occupancies, to obtain a new bitstring.
- Collect all of the old and new bitstrings that satisfy the symmetries, and subsample subsets of a fixed size, chosen in advance.
- For each subset of bitstrings, project the Hamiltonian into the subspace spanned by the corresponding basis vectors (see the previous section for a description of these basis vectors), and compute a ground state estimate of the projected Hamiltonian on a classical computer.
- Update the estimate of the average orbital occupancies with the ground state estimate with the lowest energy.
The SQD workflow is depicted in the following diagram:
SQD is known to work well when the target eigenstate is sparse: the wave function is supported in a set of basis states whose size does not increase exponentially with the size of the problem.
Requirements
Before starting this tutorial, ensure that you have the following installed:
- Qiskit SDK 1.0 or later with visualization support (
pip install 'qiskit[visualization]' ) - Qiskit Runtime 0.22 or later (
pip install qiskit-ibm-runtime ) - Qiskit Addons SQD (
pip install qiskit-addon-sqd ) - ffsim (
pip install ffsim )
Step 1: Map classical inputs to a quantum problem
In this tutorial, we will find an approximation to the ground state of the molecule at equilibrium in the 6-31G basis set. First, we specify the molecule and its properties.
Output:
converged SCF energy = -108.835236570774
CASCI E = -109.046671778080 E(CI) = -32.8155692383188 S^2 = 0.0000000
Before constructing the LUCJ ansatz circuit, we first perform a CCSD calculation in the following code cell. The and amplitudes from this calculation will be used to initialize the parameters of the ansatz.
Output:
E(CCSD) = -109.0398256929733 E_corr = -0.204589122198831
<class 'pyscf.cc.ccsd.CCSD'> does not have attributes converged
Now, we use ffsim to create the ansatz circuit. Since our molecule has a closed-shell Hartree-Fock state, we use the spin-balanced variant of the UCJ ansatz, UCJOpSpinBalanced. We pass interaction pairs appropriate for a heavy-hex lattice qubit topology (see the background section on the LUCJ ansatz).
No output produced
Step 2: Optimize problem for quantum execution
Next, we optimize the circuit for a target hardware. We'll use the 127-qubit IBM Sherbrooke QPU.
No output produced
We recommend the following steps to optimize the ansatz and make it hardware-compatible.
- Select physical qubits (
initial_layout ) from the target hardware that adheres to the zig-zag pattern described above. Laying out qubits in this pattern leads to an efficient hardware-compatible circuit with less gates. - Generate a staged pass manager using the generate_preset_pass_manager function from qiskit with your choice of
backend andinitial_layout . - Set the
pre_init stage of your staged pass manager toffsim.qiskit.PRE_INIT .ffsim.qiskit.PRE_INIT includes qiskit transpiler passes that decompose gates into orbital rotations and then merges the orbital rotations, resulting in fewer gates in the final circuit. - Run the pass manager on your circuit.
Output:
Gate counts (w/o pre-init passes): OrderedDict([('rz', 4505), ('sx', 3427), ('ecr', 1366), ('x', 207), ('measure', 32), ('barrier', 1)])
Gate counts (w/ pre-init passes): OrderedDict([('rz', 2475), ('sx', 2146), ('ecr', 730), ('x', 77), ('measure', 32), ('barrier', 1)])
Step 3: Execute using Qiskit Primitives
After optimizing the circuit for hardware execution, we are ready to run it on the target hardware and collect samples for ground state energy estimation. As we only have one circuit, we will use Qiskit Runtime's Job execution mode and execute our circuit.
No output produced
No output produced
Step 4: Post-process and return result to desired classical format
SQD relies significantly on post-processing the bitstrings collected from the quantum computer. To prepare for post-processing, we will convert the counts dictionary to a bit array, which is an matrix of zeros and ones, where is the number of shots and is the number of qubits.
Note that in Qiskit's bit ordering convention, bitstrings are indexed starting from the right. Therefore, the bit array holds the measurement results of the first qubit in the rightmost column (the column with index ). Furthermore, the spin- orbitals are indexed before the spin- orbitals, which means that the spin- orbitals are to the right of the spin- orbitals.
No output produced
Recall that the self-consistent configuration recovery is an iterative procedure that runs in a loop. In the following code cell, the first iteration of the loop simply uses the raw samples (after postselection on symmetries) as input to the diagonalization procedure to obtain an estimate of the average orbital occupancies. Later iterations of the loop use these occupancies to generate new configurations from raw samples that violate the symmetries. These configurations are collected and then subsampled to produce batches of configurations, which are then used to project the Hamiltonian and compute a ground state estimate with an eigenstate solver.
There are a few user-controlled options which are important for this technique:
iterations : Number of iterations of the self-consistent recovery loop.n_batches : Number of batches of configurations to subsample (this will be the number of separate calls to the eigenstate solver)samples_per_batch : Number of unique configurations to include in each batchmax_davidson_cycles : Maximum number of Davidson cycles run by the eigenstate solver
Output:
Starting configuration recovery iteration 0
Starting configuration recovery iteration 1
Starting configuration recovery iteration 2
Starting configuration recovery iteration 3
Starting configuration recovery iteration 4
Visualize the results
The first plot shows that after a couple of iterations we estimate the ground state energy within
The second plot shows the average occupancy of each spatial orbital after the final iteration. We can see that both the spin-up and spin-down electrons occupy the first five orbitals with high probability in our solutions.
Output:
Tutorial Survey
Tutorial Survey Please take one minute to provide feedback on this tutorial. Your insights will help us improve our content offerings and user experience. |
Was this page helpful?