Skip to content

Commit ceb9dbd

Browse files
committed
[WIP] Fixing JSON serialization
1 parent 2be2980 commit ceb9dbd

File tree

5 files changed

+21
-59
lines changed

5 files changed

+21
-59
lines changed

cirq-core/cirq/protocols/json_test_data/CZTargetGateset.json

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,12 @@
22
{
33
"cirq_type": "CZTargetGateset",
44
"atol": 1e-06,
5-
"allow_partial_czs": false,
6-
"additional_gates": [
7-
{
8-
"cirq_type": "GateFamily",
9-
"gate": "GlobalPhaseGate",
10-
"name": "Type GateFamily: cirq.ops.global_phase_op.GlobalPhaseGate",
11-
"description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.global_phase_op.GlobalPhaseGate)`",
12-
"ignore_global_phase": true,
13-
"tags_to_accept": [],
14-
"tags_to_ignore": []
15-
}
16-
]
5+
"allow_partial_czs": false
176
},
187
{
198
"cirq_type": "CZTargetGateset",
209
"atol": 1e-08,
21-
"allow_partial_czs": true,
22-
"additional_gates": [
23-
{
24-
"cirq_type": "GateFamily",
25-
"gate": "GlobalPhaseGate",
26-
"name": "Type GateFamily: cirq.ops.global_phase_op.GlobalPhaseGate",
27-
"description": "Accepts `cirq.Gate` instances `g` s.t. `isinstance(g, cirq.ops.global_phase_op.GlobalPhaseGate)`",
28-
"ignore_global_phase": true,
29-
"tags_to_accept": [],
30-
"tags_to_ignore": []
31-
}
32-
]
10+
"allow_partial_czs": true
3311
},
3412
{
3513
"cirq_type": "CZTargetGateset",

cirq-core/cirq/protocols/json_test_data/CZTargetGateset.repr

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
[
2-
cirq.CZTargetGateset(
3-
atol=1e-06,
4-
allow_partial_czs=False,
5-
additional_gates=[cirq.ops.global_phase_op.GlobalPhaseGate],
6-
),
7-
cirq.CZTargetGateset(
8-
atol=1e-08,
9-
allow_partial_czs=True,
10-
additional_gates=[cirq.ops.global_phase_op.GlobalPhaseGate],
11-
),
2+
cirq.CZTargetGateset(atol=1e-06, allow_partial_czs=False, additional_gates=[]),
3+
cirq.CZTargetGateset(atol=1e-08, allow_partial_czs=True, additional_gates=[]),
124
cirq.CZTargetGateset(
135
atol=1e-06,
146
allow_partial_czs=True,

cirq-core/cirq/transformers/target_gatesets/cz_gateset.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class CZTargetGateset(compilation_target_gateset.TwoQubitCompilationTargetGatese
3232
- `cirq.CZ` / `cirq.CZPowGate`: The two qubit entangling gate.
3333
- `cirq.PhasedXZGate`: Single qubit rotations.
3434
- `cirq.MeasurementGate`: Measurements.
35+
- `cirq.GlobalPhaseGate`: Global phase.
3536
3637
Optionally, users can also specify additional gates / gate families which should
3738
be accepted by this gateset via the `additional_gates` argument.
@@ -46,9 +47,7 @@ def __init__(
4647
*,
4748
atol: float = 1e-8,
4849
allow_partial_czs: bool = False,
49-
additional_gates: Sequence[Union[Type['cirq.Gate'], 'cirq.Gate', 'cirq.GateFamily']] = (
50-
ops.GlobalPhaseGate,
51-
),
50+
additional_gates: Sequence[Union[Type['cirq.Gate'], 'cirq.Gate', 'cirq.GateFamily']] = (),
5251
) -> None:
5352
"""Initializes CZTargetGateset
5453
@@ -63,6 +62,7 @@ def __init__(
6362
ops.CZPowGate if allow_partial_czs else ops.CZ,
6463
ops.MeasurementGate,
6564
ops.PhasedXZGate,
65+
ops.GlobalPhaseGate,
6666
*additional_gates,
6767
name='CZPowTargetGateset' if allow_partial_czs else 'CZTargetGateset',
6868
)
@@ -99,16 +99,13 @@ def _value_equality_values_(self) -> Any:
9999
return self.atol, self.allow_partial_czs, frozenset(self.additional_gates)
100100

101101
def _json_dict_(self) -> Dict[str, Any]:
102-
return {
103-
'atol': self.atol,
104-
'allow_partial_czs': self.allow_partial_czs,
105-
'additional_gates': list(self.additional_gates),
106-
}
102+
d: Dict[str, Any] = {'atol': self.atol, 'allow_partial_czs': self.allow_partial_czs}
103+
if self.additional_gates:
104+
d['additional_gates'] = list(self.additional_gates)
105+
return d
107106

108107
@classmethod
109-
def _from_json_dict_(
110-
cls, atol, allow_partial_czs, additional_gates=(ops.GlobalPhaseGate,), **kwargs
111-
):
108+
def _from_json_dict_(cls, atol, allow_partial_czs, additional_gates=(), **kwargs):
112109
return cls(
113110
atol=atol, allow_partial_czs=allow_partial_czs, additional_gates=additional_gates
114111
)

cirq-core/cirq/transformers/target_gatesets/sqrt_iswap_gateset.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class SqrtIswapTargetGateset(compilation_target_gateset.TwoQubitCompilationTarge
3333
- `cirq.SQRT_ISWAP` / `cirq.SQRT_ISWAP_INV`: The two qubit entangling gate.
3434
- `cirq.PhasedXZGate`: Single qubit rotations.
3535
- `cirq.MeasurementGate`: Measurements.
36+
- `cirq.GlobalPhaseGate`: Global phase.
3637
3738
Optionally, users can also specify additional gates / gate families which should
3839
be accepted by this gateset via the `additional_gates` argument.
@@ -48,9 +49,7 @@ def __init__(
4849
atol: float = 1e-8,
4950
required_sqrt_iswap_count: Optional[int] = None,
5051
use_sqrt_iswap_inv: bool = False,
51-
additional_gates: Sequence[Union[Type['cirq.Gate'], 'cirq.Gate', 'cirq.GateFamily']] = (
52-
ops.GlobalPhaseGate,
53-
),
52+
additional_gates: Sequence[Union[Type['cirq.Gate'], 'cirq.Gate', 'cirq.GateFamily']] = (),
5453
):
5554
"""Initializes `cirq.SqrtIswapTargetGateset`
5655
@@ -74,6 +73,7 @@ def __init__(
7473
ops.SQRT_ISWAP_INV if use_sqrt_iswap_inv else ops.SQRT_ISWAP,
7574
ops.MeasurementGate,
7675
ops.PhasedXZGate,
76+
ops.GlobalPhaseGate,
7777
*additional_gates,
7878
name='SqrtIswapInvTargetGateset' if use_sqrt_iswap_inv else 'SqrtIswapTargetGateset',
7979
)
@@ -124,21 +124,18 @@ def _value_equality_values_(self) -> Any:
124124
)
125125

126126
def _json_dict_(self) -> Dict[str, Any]:
127-
return {
127+
d: Dict[str, Any] = {
128128
'atol': self.atol,
129129
'required_sqrt_iswap_count': self.required_sqrt_iswap_count,
130130
'use_sqrt_iswap_inv': self.use_sqrt_iswap_inv,
131-
'additional_gates': list(self.additional_gates),
132131
}
132+
if self.additional_gates:
133+
d['additional_gates'] = list(self.additional_gates)
134+
return d
133135

134136
@classmethod
135137
def _from_json_dict_(
136-
cls,
137-
atol,
138-
required_sqrt_iswap_count,
139-
use_sqrt_iswap_inv,
140-
additional_gates=(ops.GlobalPhaseGate,),
141-
**kwargs,
138+
cls, atol, required_sqrt_iswap_count, use_sqrt_iswap_inv, additional_gates=(), **kwargs
142139
):
143140
return cls(
144141
atol=atol,

cirq-core/cirq/transformers/target_gatesets/sqrt_iswap_gateset_test.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ def test_sqrt_iswap_gateset_raises():
149149
def test_sqrt_iswap_gateset_eq():
150150
eq = cirq.testing.EqualsTester()
151151
eq.add_equality_group(
152-
cirq.SqrtIswapTargetGateset(),
153-
cirq.SqrtIswapTargetGateset(use_sqrt_iswap_inv=False),
154-
cirq.SqrtIswapTargetGateset(additional_gates=[cirq.GlobalPhaseGate]),
152+
cirq.SqrtIswapTargetGateset(), cirq.SqrtIswapTargetGateset(use_sqrt_iswap_inv=False)
155153
)
156154
eq.add_equality_group(
157155
cirq.SqrtIswapTargetGateset(atol=1e-6, required_sqrt_iswap_count=0, use_sqrt_iswap_inv=True)

0 commit comments

Comments
 (0)