Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
20 changes: 16 additions & 4 deletions cirq-core/cirq/contrib/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""Functions for JSON serialization and de-serialization for classes in Contrib."""

from __future__ import annotations

import functools

from cirq.protocols.json_serialization import _register_resolver, DEFAULT_RESOLVERS, ObjectFactory
Expand Down Expand Up @@ -38,7 +37,20 @@ def _class_resolver_dictionary() -> dict[str, ObjectFactory]:
]
return {cls.__name__: cls for cls in classes}


DEFAULT_CONTRIB_RESOLVERS = [contrib_class_resolver] + DEFAULT_RESOLVERS

_register_resolver(_class_resolver_dictionary)

DEFAULT_CONTRIB_RESOLVERS = [contrib_class_resolver, *DEFAULT_RESOLVERS]

_DEFAULT_CONTRIB_RESOLVERS_DEPRECATION_MESSAGE = (
'DEFAULT_CONTRIB_RESOLVERS will no longer be supported.'
'Contrib classes are now automatically resolved through the standard JSON resolver.'
'You can remove the "resolvers" parameter from assert_json_roundtrip_works calls.'
)
from cirq import _compat

_compat.deprecate_attributes(
__name__,
{
'DEFAULT_CONTRIB_RESOLVERS': ('v1.8', _DEFAULT_CONTRIB_RESOLVERS_DEPRECATION_MESSAGE),
},
)
33 changes: 0 additions & 33 deletions cirq-core/cirq/contrib/json_test.py

This file was deleted.

10 changes: 10 additions & 0 deletions cirq-core/cirq/contrib/json_test_data/BayesianNetworkGate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"cirq_type": "BayesianNetworkGate",
"init_probs": [
["q0", 0.125],
["q1", null]
],
"arc_probs": [
["q1", ["q0"], [0.25, 0.5]]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq.contrib.bayesian_network.BayesianNetworkGate(init_probs=[('q0', 0.125), ('q1', None)], arc_probs=[('q1', ('q0',), [0.25, 0.5])])
169 changes: 169 additions & 0 deletions cirq-core/cirq/contrib/json_test_data/QuantumVolumeResult.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"cirq_type": "QuantumVolumeResult",
"model_circuit": {
"cirq_type": "Circuit",
"moments": [
{
"cirq_type": "Moment",
"operations": [
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 0
}
]
},
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 1
}
]
},
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 2
}
]
},
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 3
}
]
},
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 4
}
]
}
]
}
]
},
"heavy_set": [
1,
2,
3
],
"compiled_circuit": {
"cirq_type": "Circuit",
"moments": [
{
"cirq_type": "Moment",
"operations": [
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 0
}
]
},
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 1
}
]
},
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 2
}
]
},
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 3
}
]
},
{
"cirq_type": "GateOperation",
"gate": {
"cirq_type": "HPowGate",
"exponent": 1.0,
"global_shift": 0.0
},
"qubits": [
{
"cirq_type": "LineQubit",
"x": 4
}
]
}
]
}
]
},
"sampler_result": 0.1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq.contrib.quantum_volume.QuantumVolumeResult(model_circuit=cirq.Circuit(cirq.Moment(cirq.H(cirq.LineQubit(0)), cirq.H(cirq.LineQubit(1)), cirq.H(cirq.LineQubit(2)), cirq.H(cirq.LineQubit(3)), cirq.H(cirq.LineQubit(4)))), heavy_set=[1, 2, 3], compiled_circuit=cirq.Circuit(cirq.Moment(cirq.H(cirq.LineQubit(0)), cirq.H(cirq.LineQubit(1)), cirq.H(cirq.LineQubit(2)), cirq.H(cirq.LineQubit(3)), cirq.H(cirq.LineQubit(4)))), sampler_result=0.1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cirq_type": "SwapPermutationGate"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq.contrib.acquaintance.SwapPermutationGate(swap_gate=cirq.SWAP)
2 changes: 0 additions & 2 deletions cirq-core/cirq/contrib/json_test_data/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@
should_not_be_serialized=["Unique", "CircuitDag"],
resolver_cache=_class_resolver_dictionary(),
deprecated={},
# TODO: #7520 - create .json and .repr for these so they can be tested here
tested_elsewhere=["QuantumVolumeResult", "SwapPermutationGate", "BayesianNetworkGate"],
)