Skip to content

Support even powers of SWAP gate in CliffordSimulator #3661

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

Merged
merged 6 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
26 changes: 19 additions & 7 deletions cirq/ops/swap_gates.py
Original file line number Diff line number Diff line change
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're already in a block that has done this check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be ActOnCliffordTableauArgs

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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