|
| 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 | +import itertools |
| 15 | +from typing import Iterable, TYPE_CHECKING |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +from numpy.typing import NDArray |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + import quimb.tensor as qtn |
| 22 | + |
| 23 | + from qualtran import Bloq, ConnectionT, Register |
| 24 | + from qualtran.simulation.classical_sim import ClassicalValT |
| 25 | + |
| 26 | + |
| 27 | +def _bits_to_classical_reg_data(reg: 'Register', bits: NDArray[np.uint8]) -> 'ClassicalValT': |
| 28 | + if reg.shape == (): |
| 29 | + return reg.dtype.from_bits([*bits.flat]) |
| 30 | + return reg.dtype.from_bits_array(np.reshape(bits, reg.shape + (reg.dtype.num_qubits,))) |
| 31 | + |
| 32 | + |
| 33 | +def _bloq_to_dense_via_classical_action(bloq: 'Bloq') -> NDArray: |
| 34 | + """Internal method to compute the tensor of a bloq using its classical action. |
| 35 | +
|
| 36 | + Args: |
| 37 | + bloq: the Bloq |
| 38 | +
|
| 39 | + Returns: |
| 40 | + an NDArray of shape (2, 2, ...) indexed by the output bits followed by input bits. |
| 41 | + """ |
| 42 | + left_qubit_counts = tuple(reg.total_bits() for reg in bloq.signature.lefts()) |
| 43 | + left_qubit_splits = np.cumsum(left_qubit_counts) |
| 44 | + |
| 45 | + n_qubits_left = sum(left_qubit_counts) |
| 46 | + n_qubits_right = sum(reg.total_bits() for reg in bloq.signature.rights()) |
| 47 | + |
| 48 | + if n_qubits_left + n_qubits_right > 40: |
| 49 | + raise ValueError(f"tensor is too large: {n_qubits_left + n_qubits_right} total qubits") |
| 50 | + |
| 51 | + matrix = np.zeros((2,) * (n_qubits_right + n_qubits_left)) |
| 52 | + |
| 53 | + for input_t in itertools.product((0, 1), repeat=n_qubits_left): |
| 54 | + *inputs_t, last = np.split(input_t, left_qubit_splits) |
| 55 | + assert np.size(last) == 0 |
| 56 | + |
| 57 | + input_kwargs = { |
| 58 | + reg.name: _bits_to_classical_reg_data(reg, bits) |
| 59 | + for reg, bits in zip(bloq.signature.lefts(), inputs_t) |
| 60 | + } |
| 61 | + output_args = bloq.call_classically(**input_kwargs) |
| 62 | + |
| 63 | + if output_args: |
| 64 | + output_t = np.concatenate( |
| 65 | + [ |
| 66 | + reg.dtype.to_bits_array(np.asarray(vals)).flat |
| 67 | + for reg, vals in zip(bloq.signature.rights(), output_args) |
| 68 | + ] |
| 69 | + ) |
| 70 | + else: |
| 71 | + output_t = np.array([]) |
| 72 | + |
| 73 | + matrix[tuple([*np.atleast_1d(output_t), *np.atleast_1d(input_t)])] = 1 |
| 74 | + |
| 75 | + return matrix |
| 76 | + |
| 77 | + |
| 78 | +def bloq_to_dense_via_classical_action(bloq: 'Bloq') -> NDArray: |
| 79 | + """Return a contracted, dense ndarray representing the bloq, using its classical action. |
| 80 | +
|
| 81 | + Args: |
| 82 | + bloq: The bloq |
| 83 | +
|
| 84 | + Raises: |
| 85 | + ValueError: if the bloq does not have a classical action. |
| 86 | + """ |
| 87 | + try: |
| 88 | + matrix = _bloq_to_dense_via_classical_action(bloq) |
| 89 | + except ValueError as e: |
| 90 | + raise ValueError(f"cannot compute tensor for {bloq}: {str(e)}") from e |
| 91 | + |
| 92 | + n_qubits_left = sum(reg.total_bits() for reg in bloq.signature.lefts()) |
| 93 | + n_qubits_right = sum(reg.total_bits() for reg in bloq.signature.rights()) |
| 94 | + |
| 95 | + shape: tuple[int, ...] |
| 96 | + if n_qubits_left == 0 and n_qubits_right == 0: |
| 97 | + shape = () |
| 98 | + elif n_qubits_left == 0 or n_qubits_right == 0: |
| 99 | + shape = (2 ** max(n_qubits_left, n_qubits_right),) |
| 100 | + else: |
| 101 | + shape = (2**n_qubits_right, 2**n_qubits_left) |
| 102 | + |
| 103 | + return matrix.reshape(shape) |
| 104 | + |
| 105 | + |
| 106 | +def my_tensors_from_classical_action( |
| 107 | + bloq: 'Bloq', incoming: dict[str, 'ConnectionT'], outgoing: dict[str, 'ConnectionT'] |
| 108 | +) -> list['qtn.Tensor']: |
| 109 | + """Returns the quimb tensors for the bloq derived from its `on_classical_vals` method. |
| 110 | +
|
| 111 | + This function has the same signature as `bloq.my_tensors`, and can be used as a |
| 112 | + replacement for it when the bloq has a known classical action. |
| 113 | + For example: |
| 114 | +
|
| 115 | + ```py |
| 116 | + class ClassicalBloq(Bloq): |
| 117 | + ... |
| 118 | +
|
| 119 | + def on_classical_vals(...): |
| 120 | + ... |
| 121 | +
|
| 122 | + def my_tensors(self, incoming, outgoing): |
| 123 | + return my_tensors_from_classical_action(self, incoming, outgoing) |
| 124 | + ``` |
| 125 | + """ |
| 126 | + import quimb.tensor as qtn |
| 127 | + |
| 128 | + def _signature_to_inds(registers: Iterable['Register'], cxns: dict[str, 'ConnectionT']): |
| 129 | + for reg in registers: |
| 130 | + for cxn in np.asarray(cxns[reg.name]).flat: |
| 131 | + for j in range(reg.dtype.num_qubits): |
| 132 | + yield cxn, j |
| 133 | + |
| 134 | + data = _bloq_to_dense_via_classical_action(bloq) |
| 135 | + incoming_inds = _signature_to_inds(bloq.signature.lefts(), incoming) |
| 136 | + outgoing_inds = _signature_to_inds(bloq.signature.rights(), outgoing) |
| 137 | + inds = [*outgoing_inds, *incoming_inds] |
| 138 | + |
| 139 | + return [qtn.Tensor(data=data, inds=inds)] |
0 commit comments