Skip to content

Commit b72a532

Browse files
Create cirq_google.InternalGate (#6194)
1 parent c93224e commit b72a532

File tree

7 files changed

+127
-1
lines changed

7 files changed

+127
-1
lines changed

cirq-google/cirq_google/__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@
8989
LinePlacementStrategy,
9090
)
9191

92-
from cirq_google.ops import CalibrationTag, FSimGateFamily, PhysicalZTag, SycamoreGate, SYC
92+
from cirq_google.ops import (
93+
CalibrationTag,
94+
FSimGateFamily,
95+
InternalGate,
96+
PhysicalZTag,
97+
SYC,
98+
SycamoreGate,
99+
)
93100

94101
from cirq_google.transformers import (
95102
known_2q_op_to_sycamore_operations,

cirq-google/cirq_google/json_resolver_cache.py

+1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ def _old_xmon(*args, **kwargs):
8585
'cirq.google.GridDevice': cirq_google.GridDevice,
8686
'cirq.google.GoogleCZTargetGateset': cirq_google.GoogleCZTargetGateset,
8787
'cirq.google.DeviceParameter': cirq_google.study.device_parameter.DeviceParameter,
88+
'InternalGate': cirq_google.InternalGate,
8889
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"cirq_type": "InternalGate",
3+
"gate_name": "CouplerDelayZ",
4+
"gate_module": "pyle.cirqtools.pyle_gates",
5+
"num_qubits": 2,
6+
"delay": 1,
7+
"zpa": 0.0,
8+
"zpl": null}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq_google.InternalGate(gate_name="CouplerDelayZ", gate_module="pyle.cirqtools.pyle_gates", num_qubits=2, delay=1, zpa=0.0, zpl=None)

cirq-google/cirq_google/ops/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
from cirq_google.ops.physical_z_tag import PhysicalZTag
2222

2323
from cirq_google.ops.sycamore_gate import SycamoreGate, SYC
24+
25+
from cirq_google.ops.internal_gate import InternalGate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright 2023 The Cirq Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Any, Dict
16+
from cirq import ops, value
17+
18+
19+
@value.value_equality
20+
class InternalGate(ops.Gate):
21+
"""InternalGate is a placeholder gate for internal gates.
22+
23+
InternalGate holds the information required to instantiate
24+
a gate of type `self.gate_name` with the arguments for the gate
25+
constructor stored in `self.gate_args`.
26+
"""
27+
28+
def __init__(
29+
self,
30+
gate_name: str,
31+
gate_module: str = 'pyle.cirqtools.pyle_gates',
32+
num_qubits: int = 1,
33+
**kwargs,
34+
):
35+
"""Instatiates an InternalGate.
36+
37+
Arguments:
38+
gate_name: Gate class name.
39+
gate_module: The module of the gate (defualt: pyle.cirqtools.pyle_gates).
40+
num_qubits: Number of qubits that the gate acts on.
41+
**kwargs: The named arguments to be passed to the gate constructor.
42+
"""
43+
self.gate_module = gate_module
44+
self.gate_name = gate_name
45+
self._num_qubits = num_qubits
46+
self.gate_args = {arg: val for arg, val in kwargs.items()}
47+
48+
def _num_qubits_(self) -> int:
49+
return self._num_qubits
50+
51+
def __str__(self):
52+
gate_args = ', '.join(f'{k}={v}' for k, v in self.gate_args.items())
53+
return f'{self.gate_module}.{self.gate_name}({gate_args})'
54+
55+
def __repr__(self) -> str:
56+
gate_args = ', '.join(f'{k}={v}' for k, v in self.gate_args.items())
57+
return (
58+
f'cirq_google.InternalGate(gate_name="{self.gate_name}", '
59+
f'gate_module="{self.gate_module}", '
60+
f'num_qubits={self._num_qubits}, {gate_args})'
61+
)
62+
63+
def _json_dict_(self) -> Dict[str, Any]:
64+
return dict(
65+
gate_name=self.gate_name,
66+
gate_module=self.gate_module,
67+
num_qubits=self._num_qubits,
68+
**self.gate_args,
69+
)
70+
71+
def _value_equality_values_(self):
72+
return (self.gate_module, self.gate_name, self._num_qubits, self.gate_args)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2023 The Cirq Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import cirq
16+
import cirq_google
17+
18+
19+
def test_internal_gate():
20+
g = cirq_google.InternalGate(
21+
gate_name="CouplerDelayZ",
22+
gate_module='pyle.cirqtools.pyle_gates',
23+
num_qubits=2,
24+
delay=1,
25+
zpa=0.0,
26+
zpl=None,
27+
)
28+
assert str(g) == 'pyle.cirqtools.pyle_gates.CouplerDelayZ(delay=1, zpa=0.0, zpl=None)'
29+
want_repr = (
30+
'cirq_google.InternalGate(gate_name="CouplerDelayZ", '
31+
'gate_module="pyle.cirqtools.pyle_gates", num_qubits=2, '
32+
'delay=1, zpa=0.0, zpl=None)'
33+
)
34+
assert repr(g) == want_repr
35+
assert cirq.qid_shape(g) == (2, 2)

0 commit comments

Comments
 (0)