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

from __future__ import annotations

import warnings
import functools

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

class _DeprecatedDefaultContribResolvers(list):
"""Wrapper to emit deprecation warning when DEFAULT_CONTRIB_RESOLVERS is accessed."""

def __init__(self):
super().__init__([contrib_class_resolver] + DEFAULT_RESOLVERS)
self._warning_shown = False

def __iter__(self):
self._show_warning()
return super().__iter__()

def __getitem__(self, item):
self._show_warning()
return super().__getitem__(item)

def _show_warning(self):
if not self._warning_shown:
warnings.warn(
"DEFAULT_CONTRIB_RESOLVERS is deprecated. "
"Contrib classes are now automatically resolved through the standard JSON resolver. "
"You can remove the 'resolvers' parameter from assert_json_roundtrip_works calls.",
DeprecationWarning,
stacklevel=4
)
self._warning_shown = True


DEFAULT_CONTRIB_RESOLVERS = _DeprecatedDefaultContribResolvers()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use _compat.deprecate_attributes with a deadline of v1.8 instead - similarly as what was done for the XMON (and others) here -

_compat.deprecate_attributes(
__name__,
{
'XMON': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE),
'FSIM_GATESET': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE),
'SQRT_ISWAP_GATESET': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE),
'SYC_GATESET': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE),
'NAMED_GATESETS': ('v0.16', _SERIALIZABLE_GATESET_DEPRECATION_MESSAGE),
},
)

The deprecate_attributes call should be the last one in this module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @pavoljuhas

I have tried creating similar to the XMON example provided, can you please review the change and let me know if it is correct or is there any fix needed for the change?

I tried running a basic run with test code but since json_test.py has been removed, I wasn't able to confirm the warning message.


DEFAULT_CONTRIB_RESOLVERS = [contrib_class_resolver] + DEFAULT_RESOLVERS

_register_resolver(_class_resolver_dictionary)
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)
3 changes: 2 additions & 1 deletion cirq-core/cirq/contrib/json_test_data/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
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"],
#tested_elsewhere=["QuantumVolumeResult", "SwapPermutationGate", "BayesianNetworkGate"],
tested_elsewhere=[]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete, tested_elsewhere is an optional argument.
Also clean up the TODO comment, it would be confusing if left in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @pavoljuhas

I have removed tested_elsewhere from the spec.py file

)