Pauli vs Majorana Propagation¶
propaq supports both Pauli and Majorana propagation. In this notebook, we'll take a look at simulating the unitary cluster Jastrow (UCJ) ansatz for a simple H2 molecule, a circuit commonly used in quantum chemistry applications.
Setup¶
import ffsim
import matplotlib.pyplot as plt; plt.rcParams.update({"font.family": "serif", "font.size": 12})
import numpy as np
import pyscf
import pyscf.cc
import pyscf.mcscf
import qiskit
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.providers.fake_provider import GenericBackendV2
atom: str = "H"
natoms: int = 6
nlayers: int = 1
def generate_linear_geometry(atom: str, natoms: int, atomic_distance: float = 1.0) -> str:
"""Returns a linear Hydrogen chain geometry for use in PySCF molecule construction.
Args:
natoms: Number of Hydrogen atoms in the chain.
atomic_distance: Equal spacing between Hydrogen atoms.
"""
return "; ".join([f"{atom} 0 0 {i * atomic_distance}" for i in range(natoms)])
# Specify molecule properties
spin_sq = 0
# Build molecule
mol = pyscf.gto.Mole()
mol.build(
atom=generate_linear_geometry(atom, natoms),
basis="sto-6g",
)
# Define active space
n_frozen = 0
active_space = range(n_frozen, mol.nao_nr())
# Get molecular integrals
scf = pyscf.scf.RHF(mol).run()
norb = len(active_space)
n_electrons = int(sum(scf.mo_occ[active_space]))
n_alpha = (n_electrons + mol.spin) // 2
n_beta = (n_electrons - mol.spin) // 2
nelec = (n_alpha, n_beta)
cas = pyscf.mcscf.CASCI(scf, norb, nelec)
mo = cas.sort_mo(active_space, base=0)
hcore, nuclear_repulsion_energy = cas.get_h1cas(mo)
eri = pyscf.ao2mo.restore(1, cas.get_h2cas(mo), norb)
print(f"norb = {norb}")
print(f"nelec = {nelec}")
# Get CCSD t2 amplitudes for initializing the ansatz
ccsd = pyscf.cc.CCSD(
scf, frozen=[i for i in range(mol.nao_nr()) if i not in active_space]
).run()
t1 = ccsd.t1
t2 = ccsd.t2
ccsd_energy = ccsd.e_tot
import warnings
from qiskit.transpiler import CouplingMap
warnings.formatwarning = lambda msg, *args, **kwargs: f"Warning: {msg}\n"
# Set ansatz properties
n_reps = nlayers
# Initialize backend, use exactly 2*norb qubits so transpilation adds no ancilla
# qubits and physical qubit indices match the molecular spin-orbital ordering.
coupling_map = CouplingMap.from_full(2 * norb, bidirectional=True)
backend = GenericBackendV2(
2 * norb,
coupling_map=coupling_map,
basis_gates=["cp", "xx_plus_yy", "p", "x", "swap"],
)
# Create the LUCJ ansatz operator
ucj_op = ffsim.UCJOpSpinBalanced.from_t_amplitudes(
t2=t2,
t1=t1,
n_reps=n_reps,
# Setting optimize=True enables the "compressed" factorization
optimize=True,
options=dict(maxiter=10_000),
)
# create an empty quantum circuit
qubits = QuantumRegister(2 * norb, name="q")
circuit = QuantumCircuit(qubits)
# prepare Hartree-Fock state as the reference state and append it to the quantum circuit
circuit.append(ffsim.qiskit.PrepareHartreeFockJW(norb, nelec), qubits)
# apply the UCJ operator to the reference state
circuit.append(ffsim.qiskit.UCJOpSpinBalancedJW(ucj_op), qubits)
compiled = qiskit.transpile(
circuit, backend=backend, optimization_level=3,
initial_layout=list(range(2 * norb)),
)
print(f"Number of qubits: {compiled.num_qubits}")
print(f"Gate counts: {compiled.count_ops()}")
compiled.draw(fold=-1)
converged SCF energy = -3.15600092954731
norb = 6 nelec = (3, 3)
E(CCSD) = -3.257214530748512 E_corr = -0.1012136012012059
An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
Number of qubits: 12
Gate counts: OrderedDict({'xx_plus_yy': 84, 'cp': 66, 'p': 28, 'x': 6})
┌───┐ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────────────┐ ┌──────────────┐ ┌──────────────────┐ ┌──────────────────┐ ┌────────────────────────────┐ ┌───────────┐ ┌───────────────────────┐ ┌──────────────────────┐
q_0 -> 0 ───────┤ X ├────────────┤1 ├───────────────────────────────────┤1 ├─────────────────────────┤1 ├──┤ P(-0.063461) ├──────────────────■───────────────────────────■────────────────────────────────────────■───────────────────────────────────────■────────────────────────────────────────────────────────■──────────────────────────────────────────────────────────────────────■──────────────────────────■─────────────────────────────────────────────────────────────────────────────────────────────────────────────────■─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────■───────────────────────────────────────────────────────────────────■──────────────────────────────────────────■───────────┤1 ├────────────────────────────────┤1 ├───────────────────────────────────────────────────┤1 ├───┤ P(3.0344) ├──────────────────────────────────────┤1 ├─────────────────────────────┤1 ├───────────────────────────
├───┤ │ (XX+YY)(π,-π/2) │ ┌─────────────────────────┐ │ (XX+YY)(π,-π/2) │ ┌──────────────────┐ │ (XX+YY)(1.1989,-4.6945) │┌─┴──────────────┴┐ ┌────────────┐ │P(0.031146) │ │ │ │ │ │ │ │ │ │ │ (XX+YY)(π,-π/2) │┌──────────────────────────┐ │ (XX+YY)(π,-π/2) │ ┌──────────────────┐│ (XX+YY)(0.099514,-1.4501) │┌──┴───────────┴──┐ ┌───────────┐ ┌──────────────────┐│ (XX+YY)(0.8159,-π/2) │ ┌─────────────────────────┐ │ (XX+YY)(0.8159,π/2) │┌─────────────────┐
q_1 -> 1 ───────┤ X ├────────────┤0 ├───┤1 ├─────┤0 ├────┤1 ├─┤0 ├┤0 ├─┤ P(-3.1649) ├──■─────────────■─────────────┼─────────────────────────■──────────────┼────────────■──────────────────────────┼──────────────■─────────────────────────────────────────┼───────────────────────────────────────────────────────■──────────────┼──────────────────────────┼─────────────■───────────────────────────────────────────■───────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────■────────────────────────────────────────────────────────────────────┼────────────■──────────────────────────────────────────────────────┼──────────────■───────────────────────────┼───────────┤0 ├┤1 ├────┤0 ├───────────────────────────────┤1 ├┤0 ├┤0 ├─┤ P(3.1416) ├─┤1 ├┤0 ├─┤1 ├─┤0 ├┤0 ├────────
├───┤ ┌───┴──────────────────┴──┐│ (XX+YY)(3.094,-4.7032) │ ┌───┴──────────────────┴───┐│ (XX+YY)(π,-π/2) │ ├──────────────────────────┤│ (XX+YY)(π,π/2) │┌┴────────────┤ │P(-0.10591) │P(0.23346) │ │ │ │ │ │ │ │ │ │ │ │ │ ┌──────────────────────────┐ │ │ │ │ │ └──────────────────┘│ (XX+YY)(2.4427,-4.5924) │ └──────────────────┘ ┌─────────────────────────┐│ (XX+YY)(π,-π/2) │└┬─────────────────────────┬─┘│ (XX+YY)(π,π/2) │ ├───────────┴┐│ (XX+YY)(π,-π/2) │└──┬──────────────────┬─┘ │ (XX+YY)(0.014908,-π/2) │ └─┬─────────────────┬──┘│ (XX+YY)(π,π/2) │┌──────┐
q_2 -> 2 ───────┤ X ├────────┤1 ├┤0 ├─┤1 ├┤0 ├─┤0 ├┤1 ├┤ P(-0.12594) ├──■─────────────■─────────────■────────────■────────────┼──────────────┼────────────┼─────────────■────────────┼──────────────┼───────────────────────────■─────────────┼───────────────────────────■───────────────────────────┼──────────────┼──────────────────────────┼─────────────┼───────────────────────────────────────────┼─────────────■─────────────────────────────────────────┼────────────■───────────────────────────────────────────────────■───────────────────────────┼──────────────■────────────────────────┤1 ├─┼────────────┼──────────────────────────────────────────────────────┼──────────────┼───────────────────────────┼───────────────────────────────┤0 ├────────────────────────────┤1 ├┤0 ├─┤0 ├──┤1 ├─┤ P(-3.1288) ├┤0 ├───┤1 ├───┤0 ├───┤0 ├───┤1 ├┤ P(π) ├
└───┘ │ (XX+YY)(1.8312,-3.927) │├─────────────────────────┴┐│ (XX+YY)(1.6264,-3.9338) │├─────────────────┬┘ │ (XX+YY)(1.5705,0.78538) │├─────────────────┤└─┬──────────┬┘ │P(-0.11735) │ │P(-0.019818) │P(0.17812) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (XX+YY)(1.7648,-1.6362) │ │ │ │ │ │ └──────────────────────────┘ ┌────────────────────────┐ │ (XX+YY)(1.9634,1.5047) │├─────────────────┬┘ │ (XX+YY)(1.5705,1.5707) │ ├─────────────────┤┌┴────────────┤├──────────────────┤ │ (XX+YY)(π,-π/2) │ └┬────────────────────────┤ │ (XX+YY)(π,π/2) │ ├─────────────────┤├──────┤
q_3 -> 3 ────────────────────┤0 ├┤1 ├┤0 ├┤0 ├──┤1 ├┤0 ├──┤ P(0.769) ├───■─────────────■─────────────■────────────┼────────────■──────────────■────────────┼─────────────┼────────────┼──────────────┼─────────────■─────────────┼─────────────┼───────────────────────────┼─────────────■─────────────┼──────────────┼──────────────────────────┼─────────────┼───────────────────────────────────────────┼─────────────┼───────────────────────────■─────────────┼────────────┼────────────■──────────────────────────────────────┼────────────■──────────────┼──────────────┼────────────■───────────┤0 ├─┼────────────┼──────────────────────────────────────────────────────┼──────────────┼───────────────────────────┼────────────────────────────────────────────────────────────┤1 ├─┤0 ├┤0 ├──┤1 ├──┤0 ├┤ P(-0.83875) ├┤1 ├───┤0 ├────┤0 ├───┤1 ├───┤0 ├┤ P(π) ├
┌──────────────────┐└─────────────────────────┘│ (XX+YY)(3.1157,-1.5728) │└───┬─────────────────┬────┘│ (XX+YY)(π,π/2) │ ┌┴──────────────────────────┤│ (XX+YY)(π,π/2) │ ┌┴──────────┴┐ │P(0.019818) │ │P(0.10591) │P(0.082795) │ │P(-0.031146) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └──────────────────────────┘ │ │ │ │ │ ┌──────────────────┐ │ (XX+YY)(2.5361,-1.37) │ └───┬─────────────────┬───┘│ (XX+YY)(π,π/2) │ ├─────────────────────────┤ │ (XX+YY)(π,π/2) │├─────────────┤│ (XX+YY)(π,-π/2) │┌──┴──────────────────┴──┐ │ (XX+YY)(0.020144,π/2) │┌──┴─────────────────┴──┐│ (XX+YY)(π,π/2) │└──────┘
q_4 -> 4 ┤1 ├───────────────────────────┤0 ├────┤0 ├─────┤1 ├─┤0 ├┤1 ├─┤ P(0.76201) ├─■──────────────■─────────────┼────────────■────────────────────────────────────────■─────────────┼────────────■──────────────┼─────────────┼─────────────┼─────────────┼────────────■──────────────┼─────────────┼─────────────┼──────────────┼──────────────────────────┼─────────────┼─────────────■─────────────────────────────┼─────────────┼─────────────■─────────────┼─────────────┼────────────┼────────────┼─────────────────────────■────────────┼────────────┼──────────────┼──────────────┼────────────┼───────────────────■────────────────────┼────────────┼────────────────────────────────■─────────────────────┼──────────────┼───────────────────────────┼───────────────────────────────────┤1 ├─────┤0 ├─────┤0 ├────┤1 ├──┤0 ├──┤1 ├┤ P(-0.78537) ├┤0 ├┤0 ├─┤1 ├┤0 ├┤1 ├────────
│ (XX+YY)(π,-π/2) │ └──────────────────────────┘ │ (XX+YY)(π,π/2) │ └─────────────────┘ │ (XX+YY)(0.10875,-4.7088) │└──┬────────────┬─┘ └────────────┘ │P(-0.094577) │P(0.21977) │P(0.36895) │P(0.094577) │ │ │P(0.13261) │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ (XX+YY)(π,-π/2) │ └────────────────────────┘ │ (XX+YY)(π,π/2) │ └─────────────────┘ │ (XX+YY)(1.1046,1.3699) │ └───┬──────────┬──┘└─────────────┘└──────────────────┘│ (XX+YY)(0.48755,-π/2) │ └────────────────────────┘│ (XX+YY)(0.48755,π/2) │└─────────────────┘
q_5 -> 5 ┤0 ├───────────────────────────────────────────────────────────┤1 ├─────────────────────────┤1 ├───┤ P(-2.7114) ├──────────────────■────────────────────────────■───────────────────────────────────────────────────────────────────■───────────────────────────■─────────────┼─────────────┼─────────────■────────────┼──────────────┼─────────────┼─────────────┼──────────────┼────────────■─────────────┼─────────────┼─────────────┼──────────────■──────────────┼─────────────┼─────────────┼─────────────┼─────────────┼────────────┼────────────┼────────────■────────────┼────────────┼────────────┼──────────────┼──────────────┼────────────┼───────────────────┼────────────────────┼────────────┼─────────────■──────────────────┼─────────────────────┼──────────────┼─────────────■─────────────┼───────────────■───────────────────┤0 ├────────────────────────────────────┤1 ├─────────────────────────┤1 ├──────┤ P(2.504) ├──────────────────────────────────────┤1 ├───────────────────────────┤1 ├───────────────────────────
└──────┬───┬───────┘ ┌──────────────────┐ ├─────────────────┴┐ └┬──────────────────────────┤ ┌┴────────────┴┐ │ │ │ │ │ │ │P(-0.3228) │ │ │P(0.031146) │ │ │ │ │ │ │ │P(0.23346) │ │ │ │ │ │ │ │P(0.17812) │ │ │ │ │P(-0.031146) │ │ │ │ │P(0.13261) ├──────────────────┤ ├─────────────────┴┐ ┌┴─────────────────────────┴─┐ ┌┴──────────┤ ├───────────────────────┬┘ └┬──────────────────────┤
q_6 -> 6 ───────┤ X ├────────────┤1 ├───────────────────────────────────┤1 ├─────────────────────────┤1 ├──┤ P(-0.063461) ├──────────────────■───────────────────────────■────────────────────────────────────────■───────────────────────────────────────■────────────────────────────┼─────────────┼─────────────■────────────┼──────────────┼─────────────┼─────────────┼──────────────■────────────┼─────────────┼─────────────■─────────────┼──────────────┼──────────────┼─────────────┼─────────────┼─────────────┼─────────────┼────────────■────────────┼────────────┼────────────┼────────────┼────────────┼──────────────┼──────────────┼────────────■───────────────────┼────────────────────┼────────────┼─────────────┼──────────────────■─────────────────────┼──────────────┼─────────────┼─────────────┼───────────────■───────────────────┤1 ├────────────────────────────────────┤1 ├───────────────────────┤1 ├───┤ P(3.0344) ├──────────────────────────────────────┤1 ├─────────────────────────────┤1 ├───────────────────────────
├───┤ │ (XX+YY)(π,-π/2) │ ┌─────────────────────────┐ │ (XX+YY)(π,-π/2) │ ┌──────────────────┐ │ (XX+YY)(1.1989,-4.6945) │┌─┴──────────────┴┐ ┌────────────┐ │P(0.031146) │ │ │ │ │ │ │ │ │ │P(-0.046716) │ │P(0.031146) │ │ │ │P(-0.10591) │ │ │ │ │ │ │ │P(-0.019818) │ │ │P(0.082795) │ │ │ │ │ │P(0.094577) │ │ (XX+YY)(π,-π/2) │ ┌──────────────────────────┐ │ (XX+YY)(π,-π/2) │ ┌──────────────────┐│ (XX+YY)(0.099514,-1.4501) │┌──┴───────────┴──┐ ┌───────────┐ ┌──────────────────┐│ (XX+YY)(0.8159,-π/2) │ ┌─────────────────────────┐ │ (XX+YY)(0.8159,π/2) │┌─────────────────┐
q_7 -> 7 ───────┤ X ├────────────┤0 ├───┤1 ├─────┤0 ├────┤1 ├─┤0 ├┤0 ├─┤ P(-3.1649) ├──■─────────────■─────────────┼─────────────────────────■──────────────┼────────────■──────────────────────────┼──────────────■─────────────┼─────────────┼─────────────┼────────────┼──────────────┼─────────────┼─────────────■───────────────────────────┼─────────────■───────────────────────────┼──────────────┼──────────────┼─────────────■─────────────┼─────────────┼─────────────┼─────────────────────────┼────────────┼────────────┼────────────┼────────────■──────────────┼──────────────┼────────────────────────────────■────────────────────┼────────────┼─────────────┼────────────────────────────────────────┼──────────────┼─────────────■─────────────┼───────────────────────────────────┤0 ├────┤1 ├────┤0 ├───┤1 ├┤0 ├┤0 ├─┤ P(3.1416) ├─┤1 ├┤0 ├─┤1 ├─┤0 ├┤0 ├────────
├───┤ ┌───┴──────────────────┴──┐│ (XX+YY)(3.094,-4.7032) │ ┌───┴──────────────────┴───┐│ (XX+YY)(π,-π/2) │ ├──────────────────────────┤│ (XX+YY)(π,π/2) │┌┴────────────┤ │P(-0.10591) │P(0.23346) │ │ │ │ │ │ │P(-0.24502) │ │ │ │P(-0.11735) │ │ │ │P(-0.10591) │ │ │P(0.23346) │ │ │P(0.10591) │ │ │ │ │ │P(0.36895) ┌──────────────────────────┐ │ │ │ └──────────────────┘ │ (XX+YY)(2.4427,-4.5924) │┌───┴──────────────────┴──┐│ (XX+YY)(π,-π/2) │└┬─────────────────────────┬─┘│ (XX+YY)(π,π/2) │ ├───────────┴┐│ (XX+YY)(π,-π/2) │└──┬──────────────────┬─┘ │ (XX+YY)(0.014908,-π/2) │ └─┬─────────────────┬──┘│ (XX+YY)(π,π/2) │┌──────┐
q_8 -> 8 ───────┤ X ├────────┤1 ├┤0 ├─┤1 ├┤0 ├─┤0 ├┤1 ├┤ P(-0.12594) ├──■─────────────■─────────────■────────────■────────────┼──────────────┼────────────┼─────────────■────────────┼──────────────┼─────────────┼─────────────■─────────────┼────────────┼──────────────┼─────────────■─────────────────────────────────────────┼─────────────────────────────────────────┼──────────────┼──────────────■───────────────────────────┼─────────────┼─────────────■─────────────────────────┼────────────┼────────────■────────────┼───────────────────────────┼──────────────┼─────────────────────────────────────────────────────┼────────────┼─────────────■───────────┤1 ├─┼──────────────┼───────────────────────────┼───────────────────────────────────────────────────────────┤0 ├┤1 ├┤0 ├─┤0 ├──┤1 ├─┤ P(-3.1288) ├┤0 ├───┤1 ├───┤0 ├───┤0 ├───┤1 ├┤ P(π) ├
└───┘ │ (XX+YY)(1.8312,-3.927) │├─────────────────────────┴┐│ (XX+YY)(1.6264,-3.9338) │├─────────────────┬┘ │ (XX+YY)(1.5705,0.78538) │├─────────────────┤└─┬──────────┬┘ │P(-0.11735) │ │P(-0.019818) │P(0.17812) │ │ │ │ │P(-0.14276) │ │ │P(-0.11735) │ │ │ │P(0.019818) │ │ │P(0.21977) │ │P(-0.019818) │ │P(0.17812) │ │ (XX+YY)(1.7648,-1.6362) │ │ │ │ ┌────────────────────────┐ └──────────────────────────┘│ (XX+YY)(1.9634,1.5047) │├─────────────────┬┘ │ (XX+YY)(1.5705,1.5707) │ ├─────────────────┤┌┴────────────┤├──────────────────┤ │ (XX+YY)(π,-π/2) │ └┬────────────────────────┤ │ (XX+YY)(π,π/2) │ ├─────────────────┤├──────┤
q_9 -> 9 ────────────────────┤0 ├┤1 ├┤0 ├┤0 ├──┤1 ├┤0 ├──┤ P(0.769) ├───■─────────────■─────────────■────────────┼────────────■──────────────■────────────┼─────────────┼────────────┼──────────────┼─────────────■───────────────────────────┼────────────┼──────────────■───────────────────────────────────────────────────────┼─────────────────────────────────────────┼──────────────┼──────────────────────────────────────────■─────────────┼───────────────────────────────────────┼────────────■─────────────────────────┼───────────────────────────■──────────────┼─────────────────────────────────────────────────────■────────────┼─────────────────────────┤0 ├─┼──────────────┼───────────────────────────┼────────────────────────────────┤1 ├─────────────────────────────┤0 ├┤0 ├──┤1 ├──┤0 ├┤ P(-0.83875) ├┤1 ├───┤0 ├────┤0 ├───┤1 ├───┤0 ├┤ P(π) ├
┌──────────────────┐└─────────────────────────┘│ (XX+YY)(3.1157,-1.5728) │└───┬─────────────────┬────┘│ (XX+YY)(π,π/2) │ ┌┴──────────────────────────┤│ (XX+YY)(π,π/2) │ ┌┴──────────┴┐ │P(0.019818) │ │P(0.10591) │P(0.082795) │ │P(-0.031146) │ │ │P(-0.046716) │ │ │P(-0.094577) │P(0.019818) │ │P(0.10591) │ │P(0.082795) └──────────────────────────┘ │P(-0.031146) │ │ ┌──────────────────┐ │ (XX+YY)(2.5361,-1.37) │ ┌─────────────────┐ └─────────────────────────┘│ (XX+YY)(π,π/2) │ ├─────────────────────────┤ │ (XX+YY)(π,π/2) │├─────────────┤│ (XX+YY)(π,-π/2) │┌──┴──────────────────┴──┐ │ (XX+YY)(0.020144,π/2) │┌──┴─────────────────┴──┐│ (XX+YY)(π,π/2) │└──────┘
q_10 -> 10 ┤1 ├───────────────────────────┤0 ├────┤0 ├─────┤1 ├─┤0 ├┤1 ├─┤ P(0.76201) ├─■──────────────■─────────────┼────────────■────────────────────────────────────────■─────────────┼────────────■──────────────┼─────────────────────────────────────────┼────────────■──────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────┼──────────────■────────────────────────────────────────────────────────■───────────────────────────────────────┼──────────────────────────────────────■──────────────────────────────────────────┼──────────────────────────────────────────────────────────────────■──────────────────────────────────────────────────────■──────────────┼───────────────────────────┼───────────┤1 ├─┤0 ├─────┤0 ├────────────────────────────────┤1 ├──┤0 ├──┤1 ├┤ P(-0.78537) ├┤0 ├┤0 ├─┤1 ├┤0 ├┤1 ├────────
│ (XX+YY)(π,-π/2) │ └──────────────────────────┘ │ (XX+YY)(π,π/2) │ └─────────────────┘ │ (XX+YY)(0.10875,-4.7088) │└──┬────────────┬─┘ └────────────┘ │P(-0.094577) │P(0.21977) │P(0.36895) │P(0.094577) │P(0.13261) │P(-0.41137) │P(-0.094577) │P(0.21977) │P(0.36895) │P(0.094577) │P(0.13261) │ (XX+YY)(π,-π/2) │ └────────────────────────┘ │ (XX+YY)(π,π/2) │ └─────────────────┘ │ (XX+YY)(1.1046,1.3699) │ └───┬──────────┬──┘└─────────────┘└──────────────────┘│ (XX+YY)(0.48755,-π/2) │ └────────────────────────┘│ (XX+YY)(0.48755,π/2) │└─────────────────┘
q_11 -> 11 ┤0 ├───────────────────────────────────────────────────────────┤1 ├─────────────────────────┤1 ├───┤ P(-2.7114) ├──────────────────■────────────────────────────■───────────────────────────────────────────────────────────────────■───────────────────────────■─────────────────────────────────────────■───────────────────────────────────────────────────────────────────────────────────■─────────────────────────────────────────■───────────────────────────────────────────────────────────────────────────────────────────────────────────────■─────────────────────────────────────────────────────────────────────────────────■────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────■───────────────────────────■───────────┤0 ├────────────────────────────────┤1 ├─────────────────────────────────────────────────────┤1 ├──────┤ P(2.504) ├──────────────────────────────────────┤1 ├───────────────────────────┤1 ├───────────────────────────
└──────────────────┘ └─────────────────┘ └───────────────────────────┘ └────────────┘ └──────────────────┘ └─────────────────┘ └─────────────────────────┘ └──────────┘ └────────────────────────┘ └───────────────────────┘
Now, let's build the Hamiltonian.
from qiskit_nature.second_q.hamiltonians import ElectronicEnergy
from qiskit_nature.second_q.operators import ElectronicIntegrals
from qiskit_nature.second_q.mappers import JordanWignerMapper
from qiskit.quantum_info import SparsePauliOp
h2e_phys = np.einsum("prqs->pqrs", eri) # chemist -> physicist notation
elec_ints = ElectronicIntegrals.from_raw_integrals(hcore, h2e_phys)
elec_hamiltonian = ElectronicEnergy(elec_ints)
mapper = JordanWignerMapper()
hamiltonian = mapper.map(elec_hamiltonian.second_q_op())
hamiltonian = (hamiltonian + SparsePauliOp("I" * (2 * norb), coeffs=[nuclear_repulsion_energy])).simplify()
sorted_indices = np.argsort(-np.abs(hamiltonian.coeffs))
hamiltonian = hamiltonian[sorted_indices]
print(f"Hamiltonian has {len(hamiltonian)} Pauli terms.")
Hamiltonian has 919 Pauli terms.
Pauli Propagation¶
We'll set up the Pauli propagation first. We won't use any noise or truncation for this example.
from propaq.datatypes import PauliTermSum
from propaq.propagators import PauliPropagator
from propaq.circuits import PauliCircuit
obs_ham = PauliTermSum.from_sparse_pauli_op(hamiltonian)
pc = PauliCircuit.from_qiskit(compiled.copy())
prop_pauli = PauliPropagator(
n_threads=16,
progress_bar=True,
)
result_pauli = prop_pauli.expectation_value(obs_ham, pc, initial_state=0) # initial state is vacuum since the circuit already includes the state preparation.
Propagating: 0%| | 0/568 [00:00<?, ?gate/s]
Majorana Propagation¶
Now, let's execute the same circuit with Majorana propagation, again without noise or truncation.
from propaq.datatypes import MajoranaTermSum
from propaq.propagators import MajoranaPropagator
from propaq.circuits import MajoranaCircuit
obs_ham = MajoranaTermSum.from_sparse_pauli_op(hamiltonian)
mc = MajoranaCircuit.from_ffsim_ucj(ucj_op, 4 * norb)
hf_state = sum(1 << k for k in range(n_alpha)) | sum(1 << (norb + k) for k in range(n_beta))
prop_majorana = MajoranaPropagator(
n_threads=16,
progress_bar=True,
)
result_majorana = prop_majorana.expectation_value(obs_ham, mc, initial_state=hf_state) # mc has no state-prep gates, so propagate from the Hartree-Fock basis state directly.
Propagating: 0%| | 0/527 [00:00<?, ?gate/s]
import matplotlib.pyplot as plt
plt.plot(result_pauli.n_terms, label="Pauli")
plt.plot(result_majorana.n_terms, label="Majorana")
plt.yscale("log")
plt.xlabel("Gate index")
plt.ylabel("Number of terms")
plt.legend()
plt.show()
Note that due to the Jordan-Wigner transformation, the Pauli propagation consistently produces a larger number of terms than the Majorana propagation, which are subsequently de-duplicated during merges. However, Majorana propagation works directly in the native algebra of the ansatz, and thus avoids the overhead of the Jordan-Wigner transformation. As we would expect, the number of terms in the final operator is the same for both propagation methods -- these are two basis representations related by a bijection (JW), therefore the number of terms in the final operator is the same.
Additionally, note that the Majorana propagation has to apply fewer gates than the Pauli propagation. This is because mapping to a qubit basis introduces additional overhead in the subsequent decomposition of the ansatz gates.
print(f"CCSD: {ccsd_energy}")
print(f"Pauli propagator: {result_pauli.expectation_value} (n_terms={result_pauli.n_terms[-1]})")
print(f"Majorana propagator: {result_majorana.expectation_value} (n_terms={result_majorana.n_terms[-1]})")
CCSD: -3.257214530748512 Pauli propagator: -3.1658580781233607 (n_terms=4194291) Majorana propagator: -3.1658580781233603 (n_terms=4194291)