Skip to content

Commit

Permalink
Added check for hybrid Control/Matrix Gate classes + test
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Boschero committed Mar 6, 2024
1 parent 6c9fceb commit 2b5ce76
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 65 deletions.
3 changes: 0 additions & 3 deletions .idea/.gitignore

This file was deleted.

14 changes: 0 additions & 14 deletions .idea/OpenSquirrel.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/inspectionProfiles/profiles_settings.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

37 changes: 16 additions & 21 deletions opensquirrel/squirrel_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@

import numpy as np

from opensquirrel.common import ATOL, X, Y, Z, normalize_angle, normalize_axis
from opensquirrel.common import (
ATOL,
X,
Y,
Z,
normalize_angle,
normalize_axis,
are_matrices_equivalent_up_to_global_phase,
)


class SquirrelIRVisitor(ABC):
Expand Down Expand Up @@ -90,6 +98,11 @@ def __init__(self, generator, arguments):
self.generator = generator
self.arguments = arguments

def __eq__(self, other):
if not isinstance(other, Gate):
return False
return _compare_gate_classes(self, other)

@property
def name(self) -> Optional[str]:
return self.generator.__name__ if self.generator else "<anonymous>"
Expand Down Expand Up @@ -171,13 +184,6 @@ def __init__(self, matrix: np.ndarray, operands: List[Qubit], generator=None, ar
self.matrix = matrix
self.operands = operands

def __eq__(self, other):
if not (isinstance(other, ControlledGate) or isinstance(other, MatrixGate)):
return False
if isinstance(other, ControlledGate):
return _compare_gate_classes(self, other)
return np.allclose(self.matrix, other.matrix)

def __repr__(self):
return f"MatrixGate(qubits={self.operands}, matrix={self.matrix})"

Expand All @@ -200,17 +206,6 @@ def __init__(self, control_qubit: Qubit, target_gate: Gate, generator=None, argu
self.control_qubit = control_qubit
self.target_gate = target_gate

def __eq__(self, other):

if not (isinstance(other, ControlledGate) or isinstance(other, MatrixGate)):
return False
if isinstance(other, MatrixGate):
return _compare_gate_classes(self, other)
if self.control_qubit != other.control_qubit:
return False

return self.target_gate == other.target_gate

def __repr__(self):
return f"ControlledGate(control_qubit={self.control_qubit}, {self.target_gate})"

Expand All @@ -225,7 +220,7 @@ def is_identity(self) -> bool:
return self.target_gate.is_identity()


def _compare_gate_classes(g1: Union[ControlledGate, MatrixGate], g2: Union[ControlledGate, MatrixGate]) -> bool:
def _compare_gate_classes(g1: Gate, g2: Gate) -> bool:

union_mapping = list(set(g1.get_qubit_operands()) | set(g2.get_qubit_operands()))

Expand All @@ -234,7 +229,7 @@ def _compare_gate_classes(g1: Union[ControlledGate, MatrixGate], g2: Union[Contr
matrix_g1 = get_matrix_after_qubit_remapping([g1], union_mapping)
matrix_g2 = get_matrix_after_qubit_remapping([g2], union_mapping)

return np.allclose(matrix_g1, matrix_g2)
return are_matrices_equivalent_up_to_global_phase(matrix_g1, matrix_g2)


def named_gate(gate_generator: Callable[..., Gate]) -> Callable[..., Gate]:
Expand Down
19 changes: 19 additions & 0 deletions test/test_squirrel_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,22 @@ def test_inverse_gate(self):
)

self.assertTrue(inverted_matrix_gate == inverted_cnot_gate)

def test_global_phase(self):
inverted_matrix_with_phase = MatrixGate(
np.array(
[
[1j, 0, 0, 0],
[0, 0, 0, 1j],
[0, 0, 1j, 0],
[0, 1j, 0, 0],
]
),
operands=[Qubit(0), Qubit(1)],
)

inverted_cnot_gate = ControlledGate(
Qubit(1), BlochSphereRotation(qubit=Qubit(0), axis=(1, 0, 0), angle=math.pi, phase=math.pi / 2)
)

self.assertTrue(inverted_matrix_with_phase == inverted_cnot_gate)

0 comments on commit 2b5ce76

Please sign in to comment.