Skip to content

Commit 8d319fa

Browse files
authored
Fix basic gates not being marked as invertible (#282)
- _TurnGate was checking for SelfInverseGate instead of ReversibleGate - HGate and SwapGate weren't implementing SelfInverseGate
1 parent 39bccf0 commit 8d319fa

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

cirq/ops/common_gates.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def trace_distance_bound(self):
8989

9090
def try_cast_to(self, desired_type):
9191
if (desired_type in [gate_features.ExtrapolatableGate,
92-
gate_features.SelfInverseGate] and
92+
gate_features.ReversibleGate] and
9393
self.can_extrapolate_effect()):
9494
return self
9595
if desired_type is gate_features.KnownMatrixGate and self.has_matrix():
@@ -240,6 +240,7 @@ def __hash__(self):
240240

241241
class HGate(gate_features.AsciiDiagrammableGate,
242242
gate_features.CompositeGate,
243+
gate_features.SelfInverseGate,
243244
gate_features.KnownMatrixGate,
244245
gate_features.SingleQubitGate):
245246
"""180 degree rotation around the X+Z axis of the Bloch sphere."""
@@ -297,6 +298,7 @@ def __repr__(self):
297298

298299
class SwapGate(gate_features.AsciiDiagrammableGate,
299300
gate_features.CompositeGate,
301+
gate_features.SelfInverseGate,
300302
gate_features.KnownMatrixGate,
301303
gate_features.TwoQubitGate,
302304
InterchangeableQubitsGate):

cirq/ops/common_gates_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_runtime_types_of_rot_gates():
138138
p = gate_type(half_turns=Symbol('a'))
139139
assert p.try_cast_to(ops.KnownMatrixGate) is None
140140
assert p.try_cast_to(ops.ExtrapolatableGate) is None
141-
assert p.try_cast_to(ops.SelfInverseGate) is None
141+
assert p.try_cast_to(ops.ReversibleGate) is None
142142
assert p.try_cast_to(ops.BoundedEffectGate) is p
143143
with pytest.raises(ValueError):
144144
_ = p.matrix()
@@ -150,7 +150,7 @@ def test_runtime_types_of_rot_gates():
150150
c = gate_type(half_turns=0.5)
151151
assert c.try_cast_to(ops.KnownMatrixGate) is c
152152
assert c.try_cast_to(ops.ExtrapolatableGate) is c
153-
assert c.try_cast_to(ops.SelfInverseGate) is c
153+
assert c.try_cast_to(ops.ReversibleGate) is c
154154
assert c.try_cast_to(ops.BoundedEffectGate) is c
155155
assert c.matrix() is not None
156156
assert c.extrapolate_effect(2) is not None

cirq/ops/reversible_composite_gate.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _reverse_operation(operation: raw_types.Operation,
3939

4040

4141
def inverse_of_invertible_op_tree(root: op_tree.OP_TREE,
42-
extensions: Extensions = Extensions()
42+
extensions: Extensions = None
4343
) -> op_tree.OP_TREE:
4444
"""Generates OP_TREE inverses.
4545
@@ -50,6 +50,8 @@ def inverse_of_invertible_op_tree(root: op_tree.OP_TREE,
5050
Returns:
5151
An OP_TREE that performs the inverse operation of the given OP_TREE.
5252
"""
53+
if extensions is None:
54+
extensions = Extensions()
5355
return op_tree.transform_op_tree(
5456
root=root,
5557
op_transformation=lambda e: _reverse_operation(e, extensions),

cirq/ops/reversible_composite_gate_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,25 @@ def default_decompose(self, qubits):
104104
pass
105105

106106
assert isinstance(Included(), ops.ReversibleCompositeGate)
107+
108+
109+
def test_works_with_basic_gates():
110+
a = ops.NamedQubit('a')
111+
b = ops.NamedQubit('b')
112+
113+
basics = [ops.X(a),
114+
ops.Y(a)**0.5,
115+
ops.Z(a),
116+
ops.CZ(a, b)**-0.25,
117+
ops.CNOT(a, b),
118+
ops.H(b),
119+
ops.SWAP(a, b)]
120+
assert list(ops.inverse_of_invertible_op_tree(basics)) == [
121+
ops.SWAP(a, b),
122+
ops.H(b),
123+
ops.CNOT(a, b),
124+
ops.CZ(a, b)**0.25,
125+
ops.Z(a),
126+
ops.Y(a)**-0.5,
127+
ops.X(a),
128+
]

0 commit comments

Comments
 (0)