|
| 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) |
0 commit comments