Skip to content

Commit

Permalink
Add tests for ansatz convenience function
Browse files Browse the repository at this point in the history
TODO: Merge with UCC ansatz convenience function eventually...
  • Loading branch information
chmwzc committed Aug 1, 2024
1 parent e55cf97 commit 17a8cd3
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/test_givens_excitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import pytest
from qibo import Circuit, gates

from qibochem.ansatz import hf_circuit
from qibochem.ansatz.givens_excitation import (
double_excitation_gate,
givens_excitation_ansatz,
givens_excitation_circuit,
)
from qibochem.ansatz.util import generate_excitations, mp2_amplitude, sort_excitations
from qibochem.driver import Molecule


def test_double_excitation_gate():
Expand Down Expand Up @@ -77,3 +80,88 @@ def test_givens_excitation_errors():
"""Input excitations are single or double?"""
with pytest.raises(NotImplementedError):
test_circuit = givens_excitation_circuit(4, list(range(6)))


def test_givens_excitation_ansatz_h2():
"""Test the default arguments of ucc_ansatz using H2"""
mol = Molecule([("H", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 0.7))])
mol.run_pyscf()

# Build control circuit
control_circuit = hf_circuit(4, 2)
excitations = ([0, 1, 2, 3], [0, 2], [1, 3])
for excitation in excitations:
control_circuit += givens_excitation_circuit(4, excitation)

test_circuit = givens_excitation_ansatz(mol)

assert all(
control.name == test.name and control.target_qubits == test.target_qubits
# for control, test in zip(list(control_circuit.queue), list(test_circuit.queue))
for control, test in zip(control_circuit.queue, test_circuit.queue)
)
# Check that number of parametrised gates is the same
assert len(control_circuit.get_parameters()) == len(test_circuit.get_parameters())

# Then check that the circuit parameters are the MP2 guess parameters
# Get the MP2 amplitudes first, then expand the list based on the excitation type
mp2_guess_amplitudes = [mp2_amplitude([0, 1, 2, 3], mol.eps, mol.tei) for _ in range(8)] # Doubles
mp2_guess_amplitudes += [0.0, 0.0] # Singles
coeffs = np.array([-0.125, 0.125, -0.125, 0.125, 0.125, -0.125, 0.125, -0.125, 1.0, 1.0])
mp2_guess_amplitudes = coeffs * np.array(mp2_guess_amplitudes)
# Need to flatten the output of circuit.get_parameters() to compare it to mp2_guess_amplitudes
test_parameters = np.array([_x for _tuple in test_circuit.get_parameters() for _x in _tuple])
assert np.allclose(mp2_guess_amplitudes, test_parameters)


def test_givens_excitation_ansatz_embedding():
"""Test the default arguments of ucc_ansatz using LiH with HF embedding applied, but without the HF circuit"""
mol = Molecule([("Li", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 1.4))])
mol.run_pyscf()
mol.hf_embedding(active=[1, 2, 5])

# Generate all possible excitations
excitations = []
for order in range(2, 0, -1):
# 2 electrons, 6 spin-orbitals
excitations += sort_excitations(generate_excitations(order, range(0, 2), range(2, 6)))
# Build control circuit
control_circuit = hf_circuit(6, 0)
for excitation in excitations:
control_circuit += givens_excitation_circuit(6, excitation)

test_circuit = givens_excitation_ansatz(mol, include_hf=False, use_mp2_guess=False)

assert all(
control.name == test.name and control.target_qubits == test.target_qubits
for control, test in zip(list(control_circuit.queue), list(test_circuit.queue))
)
# Check that number of parametrised gates is the same
assert len(control_circuit.get_parameters()) == len(test_circuit.get_parameters())

# Check that the circuit parameters are all zeros
test_parameters = np.array([_x for _tuple in test_circuit.get_parameters() for _x in _tuple])
assert np.allclose(test_parameters, np.zeros(len(test_parameters)))


def test_ucc_ansatz_excitations():
"""Test the `excitations` argument of ucc_ansatz"""
mol = Molecule([("Li", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 1.4))])
mol.run_pyscf()
mol.hf_embedding(active=[1, 2, 5])

# Generate all possible excitations
excitations = [[0, 1, 2, 3], [0, 1, 4, 5]]
# Build control circuit
control_circuit = hf_circuit(6, 2)
for excitation in excitations:
control_circuit += givens_excitation_circuit(6, excitation)

test_circuit = givens_excitation_ansatz(mol, excitations=excitations)

assert all(
control.name == test.name and control.target_qubits == test.target_qubits
for control, test in zip(list(control_circuit.queue), list(test_circuit.queue))
)
# Check that number of parametrised gates is the same
assert len(control_circuit.get_parameters()) == len(test_circuit.get_parameters())

0 comments on commit 17a8cd3

Please sign in to comment.