|
| 1 | +# Copyright 2023 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 | +from functools import cached_property |
| 15 | +from typing import Any, Dict, Tuple, TYPE_CHECKING |
| 16 | + |
| 17 | +import attrs |
| 18 | +import numpy as np |
| 19 | +from attrs import frozen |
| 20 | + |
| 21 | +from qualtran import Bloq, QDType, Register, Side, Signature, SoquetT |
| 22 | +from qualtran.bloqs.bookkeeping._bookkeeping_bloq import _BookkeepingBloq |
| 23 | + |
| 24 | +if TYPE_CHECKING: |
| 25 | + import quimb.tensor as qtn |
| 26 | + |
| 27 | + from qualtran.cirq_interop import CirqQuregT |
| 28 | + from qualtran.simulation.classical_sim import ClassicalValT |
| 29 | + |
| 30 | + |
| 31 | +@frozen |
| 32 | +class Cast(_BookkeepingBloq): |
| 33 | + """Cast a register from one n-bit QDType to another QDType. |
| 34 | +
|
| 35 | +
|
| 36 | + Args: |
| 37 | + inp_dtype: Input QDType to cast from. |
| 38 | + out_dtype: Output QDType to cast to. |
| 39 | + shape: shape of the register to cast. |
| 40 | +
|
| 41 | + Registers: |
| 42 | + in: input register to cast from. |
| 43 | + out: input register to cast to. |
| 44 | + """ |
| 45 | + |
| 46 | + inp_dtype: QDType |
| 47 | + out_dtype: QDType |
| 48 | + shape: Tuple[int, ...] = attrs.field( |
| 49 | + default=tuple(), converter=lambda v: (v,) if isinstance(v, int) else tuple(v) |
| 50 | + ) |
| 51 | + |
| 52 | + def __attrs_post_init__(self): |
| 53 | + if isinstance(self.inp_dtype.num_qubits, int): |
| 54 | + if self.inp_dtype.num_qubits != self.out_dtype.num_qubits: |
| 55 | + raise ValueError("Casting only permitted between same sized registers.") |
| 56 | + |
| 57 | + def adjoint(self) -> 'Bloq': |
| 58 | + return Cast(inp_dtype=self.out_dtype, out_dtype=self.inp_dtype) |
| 59 | + |
| 60 | + @cached_property |
| 61 | + def signature(self) -> Signature: |
| 62 | + return Signature( |
| 63 | + [ |
| 64 | + Register('reg', dtype=self.inp_dtype, shape=self.shape, side=Side.LEFT), |
| 65 | + Register('reg', dtype=self.out_dtype, shape=self.shape, side=Side.RIGHT), |
| 66 | + ] |
| 67 | + ) |
| 68 | + |
| 69 | + def add_my_tensors( |
| 70 | + self, |
| 71 | + tn: 'qtn.TensorNetwork', |
| 72 | + tag: Any, |
| 73 | + *, |
| 74 | + incoming: Dict[str, 'SoquetT'], |
| 75 | + outgoing: Dict[str, 'SoquetT'], |
| 76 | + ): |
| 77 | + import quimb.tensor as qtn |
| 78 | + |
| 79 | + tn.add( |
| 80 | + qtn.Tensor( |
| 81 | + data=np.eye(2**self.inp_dtype.num_qubits, 2**self.inp_dtype.num_qubits), |
| 82 | + inds=[outgoing['reg']] + [incoming['reg']], |
| 83 | + tags=['Cast', tag], |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + def on_classical_vals(self, reg: int) -> Dict[str, 'ClassicalValT']: |
| 88 | + # TODO: Actually cast the values https://github.com/quantumlib/Qualtran/issues/734 |
| 89 | + return {'reg': reg} |
| 90 | + |
| 91 | + def as_cirq_op(self, qubit_manager, reg: 'CirqQuregT') -> Tuple[None, Dict[str, 'CirqQuregT']]: |
| 92 | + return None, {'reg': reg} |
0 commit comments