Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to support inverse gate JSON serialization #6838

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions cirq-core/cirq/json_resolver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,5 @@ def _symmetricalqidpair(qids):
'sympy.EulerGamma': lambda: sympy.EulerGamma,
'complex': complex,
'datetime.datetime': _datetime,
'_InverseCompositeGate': cirq.inverse,
}
1 change: 0 additions & 1 deletion cirq-core/cirq/ops/gate_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ def all_subclasses(cls):
cirq.transformers.measurement_transformers._ConfusionChannel,
cirq.transformers.measurement_transformers._ModAdd,
cirq.transformers.routing.visualize_routed_circuit._SwapPrintGate,
cirq.ops.raw_types._InverseCompositeGate,
cirq.circuits.qasm_output.QasmTwoQubitGate,
cirq.ops.MSGate,
# Interop gates
Expand Down
4 changes: 4 additions & 0 deletions cirq-core/cirq/ops/raw_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ class _InverseCompositeGate(Gate):

def __init__(self, original: Gate) -> None:
self._original = original
self.val = original

def _qid_shape_(self):
return protocols.qid_shape(self._original)
Expand Down Expand Up @@ -1046,6 +1047,9 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return f'{self._original!s}†'

def _json_dict_(self) -> Dict[str, Any]:
return protocols.obj_to_dict_helper(self, attribute_names=["val"])


def _validate_qid_shape(val: Any, qubits: Sequence['cirq.Qid']) -> None:
"""Helper function to validate qubits for gates and operations.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"cirq_type": "_InverseCompositeGate",
"val": {
"cirq_type": "QubitPermutationGate",
"permutation": [
0,
1,
2
]}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(cirq.QubitPermutationGate(permutation=(0, 1, 2))**-1)