Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Givens gate #135

Merged
merged 6 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tangelo/toolboxes/ansatz_generator/ansatz_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,26 @@ def derangement_circuit(qubit_list, control=None, n_qubits=None, decomp=None):
control=control)]

return Circuit(gate_list, n_qubits=n_qubits)


def givens_gate(target, theta, variational=False):
"""Generates the list of gates corresponding to a givens rotation exp(-theta*(XX+YY))

Explicitly the two-qubit matrix is
[[0, 0, 0, 0],
[0, cos(theta), -sin(theta), 0],
[0, sin(theta), cos(theta), 0],
[0, 0, 0, 0]]

Args:
target (list): list of two integers that indicate which qubits are involved in the givens rotation
theta (float): the rotation angle
variational (bool): Whether the rotation angle is a variational parameter.

Returns:
list of Gate: The list of gates corresponding to the givens rotation"""
if len(target) != 2:
alexfleury-sb marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError("target must be a list or array of two integers")
return [Gate("CNOT", target=target[0], control=target[1]),
Gate("CRY", target=target[1], control=target[0], parameter=-theta, variational=variational),
Gate("CNOT", target=target[0], control=target[1])]
19 changes: 18 additions & 1 deletion tangelo/toolboxes/ansatz_generator/tests/test_ansatz_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tangelo.molecule_library import mol_H4_sto3g
from tangelo.linq.tests.test_simulator import assert_freq_dict_almost_equal
from tangelo.toolboxes.qubit_mappings.statevector_mapping import get_reference_circuit
from tangelo.toolboxes.ansatz_generator.ansatz_utils import trotterize, get_qft_circuit
from tangelo.toolboxes.ansatz_generator.ansatz_utils import givens_gate, trotterize, get_qft_circuit
from tangelo.toolboxes.ansatz_generator.ansatz_utils import controlled_swap_to_XX_gates
from tangelo.toolboxes.ansatz_generator.ansatz_utils import derangement_circuit, controlled_pauliwords

Expand Down Expand Up @@ -302,6 +302,23 @@ def test_derangement_circuit_by_estimating_pauli_string(self):
measured = sim.get_expectation_value(exp_op, rho3_pa_circuit, initial_statevector=full_start_vec)
self.assertAlmostEqual(measured, exact, places=6)

def test_givens_gate(self):
"""Test of givens gate decomposition into 2 CNOTs and a CRY gate."""
theta = 0.3

# Explicit definition of givens rotation gate
mat_rep = np.eye(4)
mat_rep[1, 1] = np.cos(theta/2)
mat_rep[1, 2] = -np.sin(theta/2)
mat_rep[2, 1] = np.sin(theta/2)
mat_rep[2, 2] = np.cos(theta/2)

# Test that explicit definition and circuit return the same state vector
vec = np.array([np.sqrt(2)/3, 2/3, np.sqrt(2)/3, 1/3])
gvec = mat_rep@vec
_, gvec2 = sim.simulate(Circuit(givens_gate([0, 1], theta)), return_statevector=True, initial_statevector=vec)
np.testing.assert_array_almost_equal(gvec, gvec2)


if __name__ == "__main__":
unittest.main()