Skip to content

Commit 77035ba

Browse files
Migrate contrib json tests to standard json test framework (#7743)
Fixes #7520 --------- Co-authored-by: Pavol Juhas <juhas@google.com>
1 parent 2bdbea8 commit 77035ba

File tree

10 files changed

+223
-38
lines changed

10 files changed

+223
-38
lines changed

cirq-core/cirq/contrib/bayesian_network/bayesian_network_gate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _generate_got_set_for_init_prob(qubit, init_prob):
7070
yield common_gates.ry(_prob_to_angle(init_prob)).on(qubit)
7171

7272

73-
@value.value_equality
73+
@value.value_equality(unhashable=True)
7474
class BayesianNetworkGate(raw_types.Gate):
7575
"""A gate that represents a Bayesian network.
7676

cirq-core/cirq/contrib/json.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import functools
77

8+
from cirq import _compat
89
from cirq.protocols.json_serialization import _register_resolver, DEFAULT_RESOLVERS, ObjectFactory
910

1011

@@ -39,6 +40,17 @@ def _class_resolver_dictionary() -> dict[str, ObjectFactory]:
3940
return {cls.__name__: cls for cls in classes}
4041

4142

42-
DEFAULT_CONTRIB_RESOLVERS = [contrib_class_resolver] + DEFAULT_RESOLVERS
43-
4443
_register_resolver(_class_resolver_dictionary)
44+
45+
DEFAULT_CONTRIB_RESOLVERS = [contrib_class_resolver, *DEFAULT_RESOLVERS]
46+
47+
_compat.deprecate_attributes(
48+
__name__,
49+
{
50+
'DEFAULT_CONTRIB_RESOLVERS': (
51+
'v1.8',
52+
'DEFAULT_CONTRIB_RESOLVERS is no longer necessary as contrib classes are now '
53+
'automatically recognized and resolved with an import of cirq.contrib.',
54+
)
55+
},
56+
)

cirq-core/cirq/contrib/json_test.py

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"cirq_type": "BayesianNetworkGate",
3+
"init_probs": [
4+
["q0", 0.125],
5+
["q1", null]
6+
],
7+
"arc_probs": [
8+
["q1", ["q0"], [0.25, 0.5]]
9+
]
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cirq.contrib.bayesian_network.BayesianNetworkGate(
2+
init_probs=[('q0', 0.125), ('q1', None)], arc_probs=[('q1', ('q0',), [0.25, 0.5])]
3+
)
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
{
2+
"cirq_type": "QuantumVolumeResult",
3+
"model_circuit": {
4+
"cirq_type": "Circuit",
5+
"moments": [
6+
{
7+
"cirq_type": "Moment",
8+
"operations": [
9+
{
10+
"cirq_type": "GateOperation",
11+
"gate": {
12+
"cirq_type": "HPowGate",
13+
"exponent": 1.0,
14+
"global_shift": 0.0
15+
},
16+
"qubits": [
17+
{
18+
"cirq_type": "LineQubit",
19+
"x": 0
20+
}
21+
]
22+
},
23+
{
24+
"cirq_type": "GateOperation",
25+
"gate": {
26+
"cirq_type": "HPowGate",
27+
"exponent": 1.0,
28+
"global_shift": 0.0
29+
},
30+
"qubits": [
31+
{
32+
"cirq_type": "LineQubit",
33+
"x": 1
34+
}
35+
]
36+
},
37+
{
38+
"cirq_type": "GateOperation",
39+
"gate": {
40+
"cirq_type": "HPowGate",
41+
"exponent": 1.0,
42+
"global_shift": 0.0
43+
},
44+
"qubits": [
45+
{
46+
"cirq_type": "LineQubit",
47+
"x": 2
48+
}
49+
]
50+
},
51+
{
52+
"cirq_type": "GateOperation",
53+
"gate": {
54+
"cirq_type": "HPowGate",
55+
"exponent": 1.0,
56+
"global_shift": 0.0
57+
},
58+
"qubits": [
59+
{
60+
"cirq_type": "LineQubit",
61+
"x": 3
62+
}
63+
]
64+
},
65+
{
66+
"cirq_type": "GateOperation",
67+
"gate": {
68+
"cirq_type": "HPowGate",
69+
"exponent": 1.0,
70+
"global_shift": 0.0
71+
},
72+
"qubits": [
73+
{
74+
"cirq_type": "LineQubit",
75+
"x": 4
76+
}
77+
]
78+
}
79+
]
80+
}
81+
]
82+
},
83+
"heavy_set": [
84+
1,
85+
2,
86+
3
87+
],
88+
"compiled_circuit": {
89+
"cirq_type": "Circuit",
90+
"moments": [
91+
{
92+
"cirq_type": "Moment",
93+
"operations": [
94+
{
95+
"cirq_type": "GateOperation",
96+
"gate": {
97+
"cirq_type": "HPowGate",
98+
"exponent": 1.0,
99+
"global_shift": 0.0
100+
},
101+
"qubits": [
102+
{
103+
"cirq_type": "LineQubit",
104+
"x": 0
105+
}
106+
]
107+
},
108+
{
109+
"cirq_type": "GateOperation",
110+
"gate": {
111+
"cirq_type": "HPowGate",
112+
"exponent": 1.0,
113+
"global_shift": 0.0
114+
},
115+
"qubits": [
116+
{
117+
"cirq_type": "LineQubit",
118+
"x": 1
119+
}
120+
]
121+
},
122+
{
123+
"cirq_type": "GateOperation",
124+
"gate": {
125+
"cirq_type": "HPowGate",
126+
"exponent": 1.0,
127+
"global_shift": 0.0
128+
},
129+
"qubits": [
130+
{
131+
"cirq_type": "LineQubit",
132+
"x": 2
133+
}
134+
]
135+
},
136+
{
137+
"cirq_type": "GateOperation",
138+
"gate": {
139+
"cirq_type": "HPowGate",
140+
"exponent": 1.0,
141+
"global_shift": 0.0
142+
},
143+
"qubits": [
144+
{
145+
"cirq_type": "LineQubit",
146+
"x": 3
147+
}
148+
]
149+
},
150+
{
151+
"cirq_type": "GateOperation",
152+
"gate": {
153+
"cirq_type": "HPowGate",
154+
"exponent": 1.0,
155+
"global_shift": 0.0
156+
},
157+
"qubits": [
158+
{
159+
"cirq_type": "LineQubit",
160+
"x": 4
161+
}
162+
]
163+
}
164+
]
165+
}
166+
]
167+
},
168+
"sampler_result": 0.1
169+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cirq.contrib.quantum_volume.QuantumVolumeResult(
2+
model_circuit=cirq.Circuit(
3+
cirq.Moment(
4+
cirq.H(cirq.LineQubit(0)),
5+
cirq.H(cirq.LineQubit(1)),
6+
cirq.H(cirq.LineQubit(2)),
7+
cirq.H(cirq.LineQubit(3)),
8+
cirq.H(cirq.LineQubit(4)),
9+
)
10+
),
11+
heavy_set=[1, 2, 3],
12+
compiled_circuit=cirq.Circuit(
13+
cirq.Moment(
14+
cirq.H(cirq.LineQubit(0)),
15+
cirq.H(cirq.LineQubit(1)),
16+
cirq.H(cirq.LineQubit(2)),
17+
cirq.H(cirq.LineQubit(3)),
18+
cirq.H(cirq.LineQubit(4)),
19+
)
20+
),
21+
sampler_result=0.1,
22+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cirq_type": "SwapPermutationGate"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq.contrib.acquaintance.SwapPermutationGate(swap_gate=cirq.SWAP)

cirq-core/cirq/contrib/json_test_data/spec.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,4 @@
2727
should_not_be_serialized=["Unique", "CircuitDag"],
2828
resolver_cache=_class_resolver_dictionary(),
2929
deprecated={},
30-
# TODO: #7520 - create .json and .repr for these so they can be tested here
31-
tested_elsewhere=["QuantumVolumeResult", "SwapPermutationGate", "BayesianNetworkGate"],
3230
)

0 commit comments

Comments
 (0)