|
| 1 | +# Copyright 2024 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 Sequence, Any, Dict, TYPE_CHECKING |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +from cirq.ops.common_gates import H, ry |
| 19 | +from cirq.ops.pauli_gates import X |
| 20 | +from cirq.ops import raw_types |
| 21 | + |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + import cirq |
| 25 | + |
| 26 | + |
| 27 | +class UniformSuperpositionGate(raw_types.Gate): |
| 28 | + r"""Creates a uniform superposition state on the states $[0, M)$ |
| 29 | + The gate creates the state $\frac{1}{\sqrt{M}}\sum_{j=0}^{M-1}\ket{j}$ |
| 30 | + (where $1\leq M \leq 2^n$), using n qubits, according to the Shukla-Vedula algorithm [SV24]. |
| 31 | + References: |
| 32 | + [SV24] |
| 33 | + [An efficient quantum algorithm for preparation of uniform quantum superposition |
| 34 | + states](https://arxiv.org/abs/2306.11747) |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__(self, m_value: int, num_qubits: int) -> None: |
| 38 | + """Initializes UniformSuperpositionGate. |
| 39 | +
|
| 40 | + Args: |
| 41 | + m_value: The number of computational basis states. |
| 42 | + num_qubits: The number of qubits used. |
| 43 | +
|
| 44 | + Raises: |
| 45 | + ValueError: If `m_value` is not a positive integer, or |
| 46 | + if `num_qubits` is not an integer greater than or equal to log2(m_value). |
| 47 | + """ |
| 48 | + if not (isinstance(m_value, int) and (m_value > 0)): |
| 49 | + raise ValueError("m_value must be a positive integer.") |
| 50 | + log_two_m_value = m_value.bit_length() |
| 51 | + |
| 52 | + if (m_value & (m_value - 1)) == 0: |
| 53 | + log_two_m_value = log_two_m_value - 1 |
| 54 | + if not (isinstance(num_qubits, int) and (num_qubits >= log_two_m_value)): |
| 55 | + raise ValueError( |
| 56 | + "num_qubits must be an integer greater than or equal to log2(m_value)." |
| 57 | + ) |
| 58 | + self._m_value = m_value |
| 59 | + self._num_qubits = num_qubits |
| 60 | + |
| 61 | + def _decompose_(self, qubits: Sequence["cirq.Qid"]) -> "cirq.OP_TREE": |
| 62 | + """Decomposes the gate into a sequence of standard gates. |
| 63 | + Implements the construction from https://arxiv.org/pdf/2306.11747. |
| 64 | + """ |
| 65 | + qreg = list(qubits) |
| 66 | + qreg.reverse() |
| 67 | + |
| 68 | + if self._m_value == 1: # if m_value is 1, do nothing |
| 69 | + return |
| 70 | + if (self._m_value & (self._m_value - 1)) == 0: # if m_value is an integer power of 2 |
| 71 | + m = self._m_value.bit_length() - 1 |
| 72 | + yield H.on_each(qreg[:m]) |
| 73 | + return |
| 74 | + k = self._m_value.bit_length() |
| 75 | + l_value = [] |
| 76 | + for i in range(self._m_value.bit_length()): |
| 77 | + if (self._m_value >> i) & 1: |
| 78 | + l_value.append(i) # Locations of '1's |
| 79 | + |
| 80 | + yield X.on_each(qreg[q_bit] for q_bit in l_value[1:k]) |
| 81 | + m_current = 2 ** (l_value[0]) |
| 82 | + theta = -2 * np.arccos(np.sqrt(m_current / self._m_value)) |
| 83 | + if l_value[0] > 0: # if m_value is even |
| 84 | + yield H.on_each(qreg[: l_value[0]]) |
| 85 | + |
| 86 | + yield ry(theta).on(qreg[l_value[1]]) |
| 87 | + |
| 88 | + for i in range(l_value[0], l_value[1]): |
| 89 | + yield H(qreg[i]).controlled_by(qreg[l_value[1]], control_values=[False]) |
| 90 | + |
| 91 | + for m in range(1, len(l_value) - 1): |
| 92 | + theta = -2 * np.arccos(np.sqrt(2 ** l_value[m] / (self._m_value - m_current))) |
| 93 | + yield ry(theta).on(qreg[l_value[m + 1]]).controlled_by( |
| 94 | + qreg[l_value[m]], control_values=[0] |
| 95 | + ) |
| 96 | + for i in range(l_value[m], l_value[m + 1]): |
| 97 | + yield H.on(qreg[i]).controlled_by(qreg[l_value[m + 1]], control_values=[0]) |
| 98 | + |
| 99 | + m_current = m_current + 2 ** (l_value[m]) |
| 100 | + |
| 101 | + def num_qubits(self) -> int: |
| 102 | + return self._num_qubits |
| 103 | + |
| 104 | + @property |
| 105 | + def m_value(self) -> int: |
| 106 | + return self._m_value |
| 107 | + |
| 108 | + def __eq__(self, other): |
| 109 | + if isinstance(other, UniformSuperpositionGate): |
| 110 | + return (self._m_value == other._m_value) and (self._num_qubits == other._num_qubits) |
| 111 | + return False |
| 112 | + |
| 113 | + def __repr__(self) -> str: |
| 114 | + return f'UniformSuperpositionGate(m_value={self._m_value}, num_qubits={self._num_qubits})' |
| 115 | + |
| 116 | + def _json_dict_(self) -> Dict[str, Any]: |
| 117 | + d = {} |
| 118 | + d['m_value'] = self._m_value |
| 119 | + d['num_qubits'] = self._num_qubits |
| 120 | + return d |
| 121 | + |
| 122 | + def __str__(self) -> str: |
| 123 | + return f'UniformSuperpositionGate(m_value={self._m_value}, num_qubits={self._num_qubits})' |
0 commit comments