diff --git a/cirq-core/cirq/contrib/json.py b/cirq-core/cirq/contrib/json.py index 8c66f12abed..fb7d9ca4ab3 100644 --- a/cirq-core/cirq/contrib/json.py +++ b/cirq-core/cirq/contrib/json.py @@ -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 @@ -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), + }, +) \ No newline at end of file diff --git a/cirq-core/cirq/contrib/json_test.py b/cirq-core/cirq/contrib/json_test.py deleted file mode 100644 index bba8b2687f1..00000000000 --- a/cirq-core/cirq/contrib/json_test.py +++ /dev/null @@ -1,33 +0,0 @@ -# pylint: disable=wrong-or-nonexistent-copyright-notice - -from __future__ import annotations - -import cirq -from cirq.contrib.acquaintance import SwapPermutationGate -from cirq.contrib.bayesian_network import BayesianNetworkGate -from cirq.contrib.json import DEFAULT_CONTRIB_RESOLVERS -from cirq.contrib.quantum_volume import QuantumVolumeResult -from cirq.testing import assert_json_roundtrip_works - - -def test_bayesian_network_gate() -> None: - gate = BayesianNetworkGate( - init_probs=[('q0', 0.125), ('q1', None)], arc_probs=[('q1', ('q0',), [0.25, 0.5])] - ) - assert_json_roundtrip_works(gate, resolvers=DEFAULT_CONTRIB_RESOLVERS) - - -def test_quantum_volume() -> None: - qubits = cirq.LineQubit.range(5) - qvr = QuantumVolumeResult( - model_circuit=cirq.Circuit(cirq.H.on_each(qubits)), - heavy_set=[1, 2, 3], - compiled_circuit=cirq.Circuit(cirq.H.on_each(qubits)), - sampler_result=0.1, - ) - assert_json_roundtrip_works(qvr, resolvers=DEFAULT_CONTRIB_RESOLVERS) - - -def test_swap_permutation_gate() -> None: - gate = SwapPermutationGate(swap_gate=cirq.SWAP) - assert_json_roundtrip_works(gate, resolvers=DEFAULT_CONTRIB_RESOLVERS) diff --git a/cirq-core/cirq/contrib/json_test_data/BayesianNetworkGate.json b/cirq-core/cirq/contrib/json_test_data/BayesianNetworkGate.json new file mode 100644 index 00000000000..6a2937aa78b --- /dev/null +++ b/cirq-core/cirq/contrib/json_test_data/BayesianNetworkGate.json @@ -0,0 +1,10 @@ +{ + "cirq_type": "BayesianNetworkGate", + "init_probs": [ + ["q0", 0.125], + ["q1", null] + ], + "arc_probs": [ + ["q1", ["q0"], [0.25, 0.5]] + ] +} \ No newline at end of file diff --git a/cirq-core/cirq/contrib/json_test_data/BayesianNetworkGate.repr b/cirq-core/cirq/contrib/json_test_data/BayesianNetworkGate.repr new file mode 100644 index 00000000000..da7f2b65dcc --- /dev/null +++ b/cirq-core/cirq/contrib/json_test_data/BayesianNetworkGate.repr @@ -0,0 +1 @@ +cirq.contrib.bayesian_network.BayesianNetworkGate(init_probs=[('q0', 0.125), ('q1', None)], arc_probs=[('q1', ('q0',), [0.25, 0.5])]) \ No newline at end of file diff --git a/cirq-core/cirq/contrib/json_test_data/QuantumVolumeResult.json b/cirq-core/cirq/contrib/json_test_data/QuantumVolumeResult.json new file mode 100644 index 00000000000..5ebaf831224 --- /dev/null +++ b/cirq-core/cirq/contrib/json_test_data/QuantumVolumeResult.json @@ -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 +} \ No newline at end of file diff --git a/cirq-core/cirq/contrib/json_test_data/QuantumVolumeResult.repr b/cirq-core/cirq/contrib/json_test_data/QuantumVolumeResult.repr new file mode 100644 index 00000000000..a6999dffe59 --- /dev/null +++ b/cirq-core/cirq/contrib/json_test_data/QuantumVolumeResult.repr @@ -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) \ No newline at end of file diff --git a/cirq-core/cirq/contrib/json_test_data/SwapPermutationGate.json b/cirq-core/cirq/contrib/json_test_data/SwapPermutationGate.json new file mode 100644 index 00000000000..9618536752f --- /dev/null +++ b/cirq-core/cirq/contrib/json_test_data/SwapPermutationGate.json @@ -0,0 +1,3 @@ +{ + "cirq_type": "SwapPermutationGate" +} \ No newline at end of file diff --git a/cirq-core/cirq/contrib/json_test_data/SwapPermutationGate.repr b/cirq-core/cirq/contrib/json_test_data/SwapPermutationGate.repr new file mode 100644 index 00000000000..72eeec124bf --- /dev/null +++ b/cirq-core/cirq/contrib/json_test_data/SwapPermutationGate.repr @@ -0,0 +1 @@ +cirq.contrib.acquaintance.SwapPermutationGate(swap_gate=cirq.SWAP) \ No newline at end of file diff --git a/cirq-core/cirq/contrib/json_test_data/spec.py b/cirq-core/cirq/contrib/json_test_data/spec.py index 343a566dca9..ac77111f236 100644 --- a/cirq-core/cirq/contrib/json_test_data/spec.py +++ b/cirq-core/cirq/contrib/json_test_data/spec.py @@ -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"], )