|
| 1 | +# Copyright 2024 Google LLC |
| 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 functools import cached_property |
| 16 | +from typing import Any, Dict, Optional, Tuple, TYPE_CHECKING |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +from attrs import frozen |
| 20 | + |
| 21 | +from qualtran import ( |
| 22 | + Bloq, |
| 23 | + bloq_example, |
| 24 | + BloqDocSpec, |
| 25 | + CompositeBloq, |
| 26 | + DecomposeTypeError, |
| 27 | + Register, |
| 28 | + Signature, |
| 29 | + SoquetT, |
| 30 | +) |
| 31 | +from qualtran.cirq_interop.t_complexity_protocol import TComplexity |
| 32 | +from qualtran.drawing import Text, TextBox, WireSymbol |
| 33 | + |
| 34 | +if TYPE_CHECKING: |
| 35 | + import cirq |
| 36 | + import quimb.tensor as qtn |
| 37 | + |
| 38 | + from qualtran.cirq_interop import CirqQuregT |
| 39 | + from qualtran.simulation.classical_sim import ClassicalValT |
| 40 | + |
| 41 | + |
| 42 | +@frozen |
| 43 | +class Identity(Bloq): |
| 44 | + r"""The identity gate on one qubit. |
| 45 | +
|
| 46 | + Registers: |
| 47 | + q: The qubit |
| 48 | + """ |
| 49 | + |
| 50 | + @cached_property |
| 51 | + def signature(self) -> 'Signature': |
| 52 | + return Signature.build(q=1) |
| 53 | + |
| 54 | + def adjoint(self) -> 'Bloq': |
| 55 | + return self |
| 56 | + |
| 57 | + def decompose_bloq(self) -> 'CompositeBloq': |
| 58 | + raise DecomposeTypeError(f"{self} is atomic") |
| 59 | + |
| 60 | + def add_my_tensors( |
| 61 | + self, |
| 62 | + tn: 'qtn.TensorNetwork', |
| 63 | + tag: Any, |
| 64 | + *, |
| 65 | + incoming: Dict[str, 'SoquetT'], |
| 66 | + outgoing: Dict[str, 'SoquetT'], |
| 67 | + ): |
| 68 | + import quimb.tensor as qtn |
| 69 | + |
| 70 | + tn.add(qtn.Tensor(data=np.eye(2), inds=(outgoing['q'], incoming['q']), tags=["I", tag])) |
| 71 | + |
| 72 | + def as_cirq_op( |
| 73 | + self, qubit_manager: 'cirq.QubitManager', q: 'CirqQuregT' # type: ignore[type-var] |
| 74 | + ) -> Tuple['cirq.Operation', Dict[str, 'CirqQuregT']]: # type: ignore[type-var] |
| 75 | + import cirq |
| 76 | + |
| 77 | + (q,) = q |
| 78 | + return cirq.I(q), {'q': np.array([q])} |
| 79 | + |
| 80 | + def _t_complexity_(self): |
| 81 | + return TComplexity() |
| 82 | + |
| 83 | + def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol': |
| 84 | + if reg is None: |
| 85 | + return Text('') |
| 86 | + return TextBox('I') |
| 87 | + |
| 88 | + def on_classical_vals(self, q: int) -> Dict[str, 'ClassicalValT']: |
| 89 | + return {'q': q} |
| 90 | + |
| 91 | + def pretty_name(self) -> str: |
| 92 | + return self.__str__() |
| 93 | + |
| 94 | + def __str__(self) -> str: |
| 95 | + return 'I' |
| 96 | + |
| 97 | + |
| 98 | +@bloq_example |
| 99 | +def _identity() -> Identity: |
| 100 | + identity = Identity() |
| 101 | + return identity |
| 102 | + |
| 103 | + |
| 104 | +_IDENTITY_DOC = BloqDocSpec( |
| 105 | + bloq_cls=Identity, |
| 106 | + import_line='from qualtran.bloqs.basic_gates import Identity', |
| 107 | + examples=[_identity], |
| 108 | +) |
0 commit comments