-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Any, Dict, List | ||
|
||
import cirq | ||
import stim | ||
|
||
|
||
@cirq.value_equality | ||
class CZSwapGate(cirq.Gate): | ||
"""Handles explaining stim's CZSWAP gates to cirq.""" | ||
|
||
def _num_qubits_(self) -> int: | ||
return 2 | ||
|
||
def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> List[str]: | ||
return ['ZSWAP', 'ZSWAP'] | ||
|
||
def _value_equality_values_(self): | ||
return () | ||
|
||
def _decompose_(self, qubits): | ||
a, b = qubits | ||
yield cirq.SWAP(a, b) | ||
yield cirq.CZ(a, b) | ||
|
||
def _stim_conversion_(self, edit_circuit: stim.Circuit, targets: List[int], **kwargs): | ||
edit_circuit.append_operation('CZSWAP', targets) | ||
|
||
def __pow__(self, power: int) -> 'CZSwapGate': | ||
if power == +1: | ||
return self | ||
if power == -1: | ||
return self | ||
return NotImplemented | ||
|
||
def __str__(self) -> str: | ||
return 'CZSWAP' | ||
|
||
def __repr__(self): | ||
return f'stimcirq.CZSwapGate()' | ||
|
||
@staticmethod | ||
def _json_namespace_() -> str: | ||
return '' | ||
|
||
def _json_dict_(self) -> Dict[str, Any]: | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import cirq | ||
import stim | ||
import stimcirq | ||
|
||
|
||
def test_stim_conversion(): | ||
a, b, c = cirq.LineQubit.range(3) | ||
|
||
cirq_circuit = cirq.Circuit( | ||
stimcirq.CZSwapGate().on(a, b), | ||
stimcirq.CZSwapGate().on(b, c), | ||
) | ||
stim_circuit = stim.Circuit( | ||
""" | ||
CZSWAP 0 1 | ||
TICK | ||
CZSWAP 1 2 | ||
TICK | ||
""" | ||
) | ||
assert stimcirq.cirq_circuit_to_stim_circuit(cirq_circuit) == stim_circuit | ||
assert stimcirq.stim_circuit_to_cirq_circuit(stim_circuit) == cirq_circuit | ||
|
||
|
||
def test_diagram(): | ||
a, b = cirq.LineQubit.range(2) | ||
cirq.testing.assert_has_diagram( | ||
cirq.Circuit( | ||
stimcirq.CZSwapGate()(a, b), | ||
stimcirq.CZSwapGate()(a, b), | ||
), | ||
""" | ||
0: ---ZSWAP---ZSWAP--- | ||
| | | ||
1: ---ZSWAP---ZSWAP--- | ||
""", | ||
use_unicode_characters=False, | ||
) | ||
|
||
|
||
def test_inverse(): | ||
a = stimcirq.CZSwapGate() | ||
assert a**+1 == a | ||
assert a**-1 == a | ||
|
||
|
||
def test_repr(): | ||
val = stimcirq.CZSwapGate() | ||
assert eval(repr(val), {"stimcirq": stimcirq}) == val | ||
|
||
|
||
def test_equality(): | ||
eq = cirq.testing.EqualsTester() | ||
eq.add_equality_group(stimcirq.CZSwapGate(), stimcirq.CZSwapGate()) | ||
|
||
|
||
def test_json_serialization(): | ||
a, b, d = cirq.LineQubit.range(3) | ||
c = cirq.Circuit( | ||
stimcirq.CZSwapGate()(a, b), | ||
stimcirq.CZSwapGate()(b, d), | ||
) | ||
json = cirq.to_json(c) | ||
c2 = cirq.read_json(json_text=json, resolvers=[*cirq.DEFAULT_RESOLVERS, stimcirq.JSON_RESOLVER]) | ||
assert c == c2 | ||
|
||
|
||
def test_json_backwards_compat_exact(): | ||
raw = stimcirq.CZSwapGate() | ||
packed = '{\n "cirq_type": "CZSwapGate"\n}' | ||
assert cirq.to_json(raw) == packed | ||
assert cirq.read_json(json_text=packed, resolvers=[*cirq.DEFAULT_RESOLVERS, stimcirq.JSON_RESOLVER]) == raw |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters