diff --git a/cirq-core/cirq/contrib/bayesian_network/bayesian_network_gate.py b/cirq-core/cirq/contrib/bayesian_network/bayesian_network_gate.py index 1e99985ec99..5e2bed9d6d8 100644 --- a/cirq-core/cirq/contrib/bayesian_network/bayesian_network_gate.py +++ b/cirq-core/cirq/contrib/bayesian_network/bayesian_network_gate.py @@ -70,7 +70,7 @@ def _generate_got_set_for_init_prob(qubit, init_prob): yield common_gates.ry(_prob_to_angle(init_prob)).on(qubit) -@value.value_equality +@value.value_equality(unhashable=True) class BayesianNetworkGate(raw_types.Gate): """A gate that represents a Bayesian network. diff --git a/cirq-core/cirq/contrib/json.py b/cirq-core/cirq/contrib/json.py index 8c66f12abed..cad71529996 100644 --- a/cirq-core/cirq/contrib/json.py +++ b/cirq-core/cirq/contrib/json.py @@ -5,6 +5,7 @@ import functools +from cirq import _compat from cirq.protocols.json_serialization import _register_resolver, DEFAULT_RESOLVERS, ObjectFactory @@ -39,6 +40,17 @@ 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] + +_compat.deprecate_attributes( + __name__, + { + 'DEFAULT_CONTRIB_RESOLVERS': ( + 'v1.8', + 'DEFAULT_CONTRIB_RESOLVERS is no longer necessary as contrib classes are now ' + 'automatically recognized and resolved with an import of cirq.contrib.', + ) + }, +) 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..fffcf67f177 --- /dev/null +++ b/cirq-core/cirq/contrib/json_test_data/BayesianNetworkGate.repr @@ -0,0 +1,3 @@ +cirq.contrib.bayesian_network.BayesianNetworkGate( + init_probs=[('q0', 0.125), ('q1', None)], arc_probs=[('q1', ('q0',), [0.25, 0.5])] +) 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..a576b73e54f --- /dev/null +++ b/cirq-core/cirq/contrib/json_test_data/QuantumVolumeResult.repr @@ -0,0 +1,22 @@ +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, +) 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"], )