|
| 1 | +# Copyright 2018 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 | +from typing import Collection, Optional, Sequence, Union, Tuple, List, Type |
| 15 | + |
| 16 | +import copy |
| 17 | +import itertools |
| 18 | + |
| 19 | +import cirq |
| 20 | + |
| 21 | + |
| 22 | +def flatten(sequence): |
| 23 | + def _flatten_aux(sequence): |
| 24 | + if isinstance(sequence, int): |
| 25 | + yield sequence |
| 26 | + else: |
| 27 | + for item in sequence: |
| 28 | + yield from _flatten_aux(item) |
| 29 | + |
| 30 | + return tuple(_flatten_aux(sequence)) |
| 31 | + |
| 32 | + |
| 33 | +class ControlValues: |
| 34 | + def __init__( |
| 35 | + self, control_values: Sequence[Union[int, Collection[int], Type['ControlValues']]] |
| 36 | + ): |
| 37 | + if len(control_values) == 0: |
| 38 | + self.nxt = None |
| 39 | + self.vals = None |
| 40 | + self.num_variables = 0 |
| 41 | + self.itr = None |
| 42 | + return |
| 43 | + self.itr = None |
| 44 | + self.nxt = None |
| 45 | + self.vals = control_values[0] |
| 46 | + self.num_variables = 0 |
| 47 | + |
| 48 | + if len(control_values) > 1: |
| 49 | + self.nxt = ControlValues(control_values[1:]) |
| 50 | + |
| 51 | + if isinstance(self.vals, int): |
| 52 | + self.vals = ((self.vals,),) |
| 53 | + |
| 54 | + if not isinstance(self.vals, ControlValues) and isinstance(self.vals[0], int): |
| 55 | + self.vals = tuple((val,) if isinstance(val, int) else tuple(val) for val in self.vals) |
| 56 | + |
| 57 | + if isinstance(control_values[0], ControlValues): |
| 58 | + aux = control_values[0].copy().And(self.nxt) |
| 59 | + self.vals, self.num_variables, self.nxt = aux.vals, aux.num_variables, aux.nxt |
| 60 | + else: |
| 61 | + self.vals = (Tuple[Tuple[int, ...], ...], self.vals) |
| 62 | + self.num_variables = len(self.vals[0]) |
| 63 | + |
| 64 | + def And(self, other): # pylint: disable=invalid-name |
| 65 | + # Cartesian product of all combinations in self x other |
| 66 | + if other is None: |
| 67 | + return |
| 68 | + if not isinstance(other, ControlValues): |
| 69 | + raise ValueError |
| 70 | + other = other.copy() |
| 71 | + cur = self |
| 72 | + while cur.nxt: |
| 73 | + cur = cur.nxt |
| 74 | + cur.nxt = other |
| 75 | + |
| 76 | + def __call__(self): |
| 77 | + return self.__iter__() |
| 78 | + |
| 79 | + def __iter__(self): |
| 80 | + nxt = self.nxt if self.nxt else lambda: [()] |
| 81 | + if self.num_variables: |
| 82 | + self.itr = itertools.product(self.vals, nxt()) |
| 83 | + else: |
| 84 | + self.itr = itertools.product(*()) |
| 85 | + return self.itr |
| 86 | + |
| 87 | + def __next__(self): |
| 88 | + for val in self.itr: |
| 89 | + yield val |
| 90 | + |
| 91 | + def copy(self): |
| 92 | + new_copy = ControlValues( |
| 93 | + [ |
| 94 | + copy.deepcopy(self.vals), |
| 95 | + ] |
| 96 | + ) |
| 97 | + new_copy.nxt = None |
| 98 | + if self.nxt: |
| 99 | + new_copy.nxt = self.nxt.copy() |
| 100 | + return new_copy |
| 101 | + |
| 102 | + def __len__(self): |
| 103 | + cur = self |
| 104 | + num_variables = 0 |
| 105 | + while cur is not None: |
| 106 | + num_variables += cur.num_variables |
| 107 | + cur = cur.nxt |
| 108 | + return num_variables |
| 109 | + |
| 110 | + def __getitem__(self, key): |
| 111 | + if isinstance(key, slice): |
| 112 | + if key != slice(None, -1, None): |
| 113 | + raise TypeError('Unsupported slicing') |
| 114 | + return self.copy().pop() |
| 115 | + key = int(key) |
| 116 | + num_variables = len(self) |
| 117 | + if not 0 <= key < num_variables: |
| 118 | + key = key % num_variables |
| 119 | + if key < 0: |
| 120 | + key += num_variables |
| 121 | + cur = self |
| 122 | + while cur.num_variables <= key: |
| 123 | + key -= cur.num_variables |
| 124 | + cur = cur.nxt |
| 125 | + return cur |
| 126 | + |
| 127 | + def __eq__(self, other): |
| 128 | + if other is None: |
| 129 | + return False |
| 130 | + if not isinstance(other, ControlValues): |
| 131 | + return self == ControlValues(other) |
| 132 | + self_values = set(flatten(A) for A in self) |
| 133 | + other_values = set(flatten(B) for B in other) |
| 134 | + return self_values == other_values |
| 135 | + |
| 136 | + def identifier(self, companions: Sequence[Union[int, 'cirq.Qid']]): |
| 137 | + companions = tuple(companions) |
| 138 | + controls = [] |
| 139 | + cur = self |
| 140 | + while cur is not None: |
| 141 | + controls.append((cur.vals, companions[: cur.num_variables])) |
| 142 | + companions = companions[cur.num_variables :] |
| 143 | + cur = cur.nxt |
| 144 | + return tuple(controls) |
| 145 | + |
| 146 | + def check_dimentionality( |
| 147 | + self, |
| 148 | + qid_shape: Optional[Union[Tuple[int, ...], List[int]]] = None, |
| 149 | + controls: Optional[Union[Tuple['cirq.Qid', ...], List['cirq.Qid']]] = None, |
| 150 | + offset=0, |
| 151 | + ): |
| 152 | + if self.num_variables == 0: |
| 153 | + return |
| 154 | + if qid_shape is None or len(qid_shape) == 0: |
| 155 | + qid_shape = [q.dimension for q in controls[: self.num_variables]] |
| 156 | + if self.vals is None: |
| 157 | + raise ValueError('vals can\'t be None.') |
| 158 | + for product in self.vals: |
| 159 | + product = flatten(product) |
| 160 | + for i in range(self.num_variables): |
| 161 | + if not 0 <= product[i] < qid_shape[i]: |
| 162 | + message = ( |
| 163 | + 'Control values <{!r}> outside of range ' 'for control qubit number <{!r}>.' |
| 164 | + ).format(product[i], i + offset) |
| 165 | + if controls is not None: |
| 166 | + message = ( |
| 167 | + 'Control values <{product[i]!r}> outside of range' |
| 168 | + ' for qubit <{controls[i]!r}>.' |
| 169 | + ) |
| 170 | + raise ValueError(message) |
| 171 | + |
| 172 | + if self.nxt is not None: |
| 173 | + self.nxt.check_dimentionality( |
| 174 | + qid_shape=qid_shape[self.num_variables :], |
| 175 | + controls=controls[self.num_variables :] if controls else None, |
| 176 | + offset=offset + self.num_variables, |
| 177 | + ) |
| 178 | + |
| 179 | + def are_same_value(self, value: int = 1): |
| 180 | + if self.vals is None: |
| 181 | + raise ValueError('vals can\'t be None.') |
| 182 | + for product in self.vals: |
| 183 | + product = flatten(product) |
| 184 | + if not all(v == value for v in product): |
| 185 | + return False |
| 186 | + if self.nxt is not None: |
| 187 | + return self.nxt.are_same_value(value) |
| 188 | + return True |
| 189 | + |
| 190 | + def arrangements(self): |
| 191 | + _arrangements = [] |
| 192 | + cur = self |
| 193 | + while cur is not None: |
| 194 | + if cur.num_variables == 1: |
| 195 | + _arrangements.append(flatten(cur.vals)) |
| 196 | + else: |
| 197 | + _arrangements.append(flatten(product) for product in cur.vals) |
| 198 | + cur = cur.nxt |
| 199 | + return _arrangements |
| 200 | + |
| 201 | + def pop(self): |
| 202 | + if self.nxt is None: |
| 203 | + return None |
| 204 | + cur = self |
| 205 | + while cur.nxt.nxt is not None: |
| 206 | + cur = cur.nxt |
| 207 | + cur.nxt = None |
| 208 | + return self |
| 209 | + |
| 210 | + |
| 211 | +class FreeVars(ControlValues): |
| 212 | + pass |
| 213 | + |
| 214 | + |
| 215 | +class ConstrainedVars(ControlValues): |
| 216 | + def __init__(self, control_values): |
| 217 | + sum_of_product = (tuple(zip(*control_values)),) |
| 218 | + super().__init__(sum_of_product) |
0 commit comments