Skip to content

Commit

Permalink
Draft code for remaining circuit ansatz
Browse files Browse the repository at this point in the history
  • Loading branch information
chmwzc committed Jul 29, 2024
1 parent 9b60610 commit 65fe973
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/qibochem/ansatz/universal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Module documentation
"""

from qibo import Circuit, gates

Check warning on line 5 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L5

Added line #L5 was not covered by tests

# Helper functions


def double_excitation_gate(excitation, theta=0.0):

Check warning on line 10 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L10

Added line #L10 was not covered by tests
"""
Decomposition of a double excitation gate into single qubit rotations and CNOTs
Args:
n_qubits (int): Number of qubits in the circuit
excitation: Iterable of orbitals involved in the excitation; must have an even number of elements
theta (float): Rotation angle. Default: 0.0
Returns:
(list): List of gates representing the decomposition of the Givens' double excitation gate
"""
sorted_orbitals = sorted(excitation)

Check warning on line 22 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L22

Added line #L22 was not covered by tests
# Check size of orbitals input
assert len(sorted_orbitals) % 2 == 0, f"{excitation} must have an even number of items"

Check warning on line 24 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L24

Added line #L24 was not covered by tests

if theta is None:
theta = 0.0

Check warning on line 27 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L26-L27

Added lines #L26 - L27 were not covered by tests

result = []
result.append(gates.CNOT(sorted_orbitals[2], sorted_orbitals[3]))
result.append(gates.CNOT(sorted_orbitals[0], sorted_orbitals[2]))
result.append(gates.H(sorted_orbitals[0]))
result.append(gates.H(sorted_orbitals[3]))
result.append(gates.CNOT(sorted_orbitals[0], sorted_orbitals[1]))
result.append(gates.CNOT(sorted_orbitals[2], sorted_orbitals[3]))
result.append(gates.RY(sorted_orbitals[0], -0.125 * theta))
result.append(gates.RY(sorted_orbitals[1], 0.125 * theta))
result.append(gates.CNOT(sorted_orbitals[0], sorted_orbitals[3]))
result.append(gates.H(sorted_orbitals[3]))
result.append(gates.CNOT(sorted_orbitals[3], sorted_orbitals[1]))
result.append(gates.RY(sorted_orbitals[0], -0.125 * theta))
result.append(gates.RY(sorted_orbitals[1], 0.125 * theta))
result.append(gates.CNOT(sorted_orbitals[2], sorted_orbitals[1]))
result.append(gates.CNOT(sorted_orbitals[2], sorted_orbitals[0]))
result.append(gates.RY(sorted_orbitals[0], 0.125 * theta))
result.append(gates.RY(sorted_orbitals[1], -0.125 * theta))
result.append(gates.CNOT(sorted_orbitals[3], sorted_orbitals[1]))
result.append(gates.H(sorted_orbitals[3]))
result.append(gates.CNOT(sorted_orbitals[0], sorted_orbitals[3]))
result.append(gates.RY(sorted_orbitals[0], 0.125 * theta))
result.append(gates.RY(sorted_orbitals[1], -0.125 * theta))
result.append(gates.CNOT(sorted_orbitals[0], sorted_orbitals[1]))
result.append(gates.CNOT(sorted_orbitals[2], sorted_orbitals[0]))
result.append(gates.H(sorted_orbitals[0]))
result.append(gates.H(sorted_orbitals[3]))
result.append(gates.CNOT(sorted_orbitals[0], sorted_orbitals[2]))
result.append(gates.CNOT(sorted_orbitals[2], sorted_orbitals[3]))
return result

Check warning on line 58 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L29-L58

Added lines #L29 - L58 were not covered by tests


# Main function
def universal_circuit(n_qubits, excitation, theta=0.0):

Check warning on line 62 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L62

Added line #L62 was not covered by tests
"""
Universal circuit ansatz from Arrazola et al. Reference: https://doi.org/10.22331/q-2022-06-20-742
Args:
n_qubits: Number of qubits in the quantum circuit
n_electrons: Number of electrons in the molecular system
Returns:
Qibo ``Circuit``: Circuit ansatz
"""
circuit = Circuit(n_qubits)
if len(excitation) == 2:
circuit.add(gates.GIVENS(excitation[0], excitation[1], theta))
elif len(excitation) == 4:
circuit.add(double_excitation_gate(excitation, theta))
return circuit

Check warning on line 78 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L73-L78

Added lines #L73 - L78 were not covered by tests


def universal_ansatz(n_qubits, excitation, n_electrons):

Check warning on line 81 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L81

Added line #L81 was not covered by tests
"""TODO: Same implementation as UCC?"""
pass

Check warning on line 83 in src/qibochem/ansatz/universal.py

View check run for this annotation

Codecov / codecov/patch

src/qibochem/ansatz/universal.py#L83

Added line #L83 was not covered by tests

0 comments on commit 65fe973

Please sign in to comment.