Skip to content

Create cirq_google.InternalGate #6194

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

Merged
merged 4 commits into from
Jul 17, 2023
Merged
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
9 changes: 8 additions & 1 deletion cirq-google/cirq_google/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@
LinePlacementStrategy,
)

from cirq_google.ops import CalibrationTag, FSimGateFamily, PhysicalZTag, SycamoreGate, SYC
from cirq_google.ops import (
CalibrationTag,
FSimGateFamily,
InternalGate,
PhysicalZTag,
SYC,
SycamoreGate,
)

from cirq_google.transformers import (
known_2q_op_to_sycamore_operations,
Expand Down
1 change: 1 addition & 0 deletions cirq-google/cirq_google/json_resolver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ def _old_xmon(*args, **kwargs):
'cirq.google.GridDevice': cirq_google.GridDevice,
'cirq.google.GoogleCZTargetGateset': cirq_google.GoogleCZTargetGateset,
'cirq.google.DeviceParameter': cirq_google.study.device_parameter.DeviceParameter,
'InternalGate': cirq_google.InternalGate,
}
8 changes: 8 additions & 0 deletions cirq-google/cirq_google/json_test_data/InternalGate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cirq_type": "InternalGate",
"gate_name": "CouplerDelayZ",
"gate_module": "pyle.cirqtools.pyle_gates",
"num_qubits": 2,
"delay": 1,
"zpa": 0.0,
"zpl": null}
1 change: 1 addition & 0 deletions cirq-google/cirq_google/json_test_data/InternalGate.repr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq_google.InternalGate(gate_name="CouplerDelayZ", gate_module="pyle.cirqtools.pyle_gates", num_qubits=2, delay=1, zpa=0.0, zpl=None)
2 changes: 2 additions & 0 deletions cirq-google/cirq_google/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
from cirq_google.ops.physical_z_tag import PhysicalZTag

from cirq_google.ops.sycamore_gate import SycamoreGate, SYC

from cirq_google.ops.internal_gate import InternalGate
72 changes: 72 additions & 0 deletions cirq-google/cirq_google/ops/internal_gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright 2023 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Dict
from cirq import ops, value


@value.value_equality
class InternalGate(ops.Gate):
"""InternalGate is a placeholder gate for internal gates.

InternalGate holds the information required to instantiate
a gate of type `self.gate_name` with the arguments for the gate
constructor stored in `self.gate_args`.
"""

def __init__(
self,
gate_name: str,
gate_module: str = 'pyle.cirqtools.pyle_gates',
num_qubits: int = 1,
**kwargs,
):
"""Instatiates an InternalGate.

Arguments:
gate_name: Gate class name.
gate_module: The module of the gate (defualt: pyle.cirqtools.pyle_gates).
num_qubits: Number of qubits that the gate acts on.
**kwargs: The named arguments to be passed to the gate constructor.
"""
self.gate_module = gate_module
self.gate_name = gate_name
self._num_qubits = num_qubits
self.gate_args = {arg: val for arg, val in kwargs.items()}

def _num_qubits_(self) -> int:
return self._num_qubits

def __str__(self):
gate_args = ', '.join(f'{k}={v}' for k, v in self.gate_args.items())
return f'{self.gate_module}.{self.gate_name}({gate_args})'

def __repr__(self) -> str:
gate_args = ', '.join(f'{k}={v}' for k, v in self.gate_args.items())
return (
f'cirq_google.InternalGate(gate_name="{self.gate_name}", '
f'gate_module="{self.gate_module}", '
f'num_qubits={self._num_qubits}, {gate_args})'
)

def _json_dict_(self) -> Dict[str, Any]:
return dict(
gate_name=self.gate_name,
gate_module=self.gate_module,
num_qubits=self._num_qubits,
**self.gate_args,
)

def _value_equality_values_(self):
return (self.gate_module, self.gate_name, self._num_qubits, self.gate_args)
35 changes: 35 additions & 0 deletions cirq-google/cirq_google/ops/internal_gate_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2023 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import cirq
import cirq_google


def test_internal_gate():
g = cirq_google.InternalGate(
gate_name="CouplerDelayZ",
gate_module='pyle.cirqtools.pyle_gates',
num_qubits=2,
delay=1,
zpa=0.0,
zpl=None,
)
assert str(g) == 'pyle.cirqtools.pyle_gates.CouplerDelayZ(delay=1, zpa=0.0, zpl=None)'
want_repr = (
'cirq_google.InternalGate(gate_name="CouplerDelayZ", '
'gate_module="pyle.cirqtools.pyle_gates", num_qubits=2, '
'delay=1, zpa=0.0, zpl=None)'
)
assert repr(g) == want_repr
assert cirq.qid_shape(g) == (2, 2)