Skip to content

Commit

Permalink
Support even powers of SWAP gate in CliffordSimulator (#3661)
Browse files Browse the repository at this point in the history
#3659 adds support for odd powers
  • Loading branch information
smitsanghavi committed Feb 1, 2021
1 parent 3c566d2 commit d8598dc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
26 changes: 19 additions & 7 deletions cirq/ops/swap_gates.py
Expand Up @@ -85,16 +85,28 @@ def _trace_distance_bound_(self) -> Optional[float]:
return None
return abs(np.sin(self._exponent * 0.5 * np.pi))

def _has_stabilizer_effect_(self) -> Optional[bool]:
if self._is_parameterized_():
return None
return self.exponent % 1 == 0

def _act_on_(self, args):
from cirq import ops, sim, protocols

if isinstance(args, sim.ActOnStabilizerCHFormArgs) and self._exponent % 2 == 1:
args.state.omega *= 1j ** (2 * self.global_shift * self._exponent)
protocols.act_on(ops.CNOT, args)
args.axes = args.axes[::-1]
protocols.act_on(ops.CNOT, args)
args.axes = args.axes[::-1]
protocols.act_on(ops.CNOT, args)
if isinstance(args, (sim.ActOnStabilizerCHFormArgs, sim.ActOnCliffordTableauArgs)):
if not self._has_stabilizer_effect_():
return NotImplemented
if isinstance(args, sim.ActOnStabilizerCHFormArgs):
args.state.omega *= 1j ** (2 * self.global_shift * self._exponent)

if self._exponent % 2 == 1:
protocols.act_on(ops.CNOT, args)
args.axes = args.axes[::-1]
protocols.act_on(ops.CNOT, args)
args.axes = args.axes[::-1]
protocols.act_on(ops.CNOT, args)

# An even exponent does not change anything except the global phase above.
return True

return NotImplemented
Expand Down
7 changes: 7 additions & 0 deletions cirq/ops/swap_gates_test.py
Expand Up @@ -75,6 +75,13 @@ def test_text_diagrams():
)


def test_swap_has_stabilizer_effect():
assert cirq.has_stabilizer_effect(cirq.SWAP)
assert cirq.has_stabilizer_effect(cirq.SWAP ** 2)
assert not cirq.has_stabilizer_effect(cirq.SWAP ** 0.5)
assert not cirq.has_stabilizer_effect(cirq.SWAP ** sympy.Symbol('foo'))


def test_swap_unitary():
# yapf: disable
np.testing.assert_almost_equal(
Expand Down
4 changes: 4 additions & 0 deletions cirq/sim/clifford/clifford_simulator_test.py
Expand Up @@ -397,13 +397,17 @@ def test_swap():
circuit = cirq.Circuit(
cirq.X(a),
cirq.SWAP(a, b),
cirq.SWAP(a, b) ** 4,
cirq.measure(a, key="a"),
cirq.measure(b, key="b"),
)
r = cirq.CliffordSimulator().sample(circuit)
assert not r["a"][0]
assert r["b"][0]

with pytest.raises(NotImplementedError, match="CliffordSimulator doesn't support"):
cirq.CliffordSimulator().simulate((cirq.Circuit(cirq.SWAP(a, b) ** 3.5)))


def test_sample_seed():
q = cirq.NamedQubit('q')
Expand Down

0 comments on commit d8598dc

Please sign in to comment.