|
13 | 13 | from scipy.stats import unitary_group
|
14 | 14 |
|
15 | 15 | from .cons import backend, dtypestr, npdtype
|
| 16 | +from .backends import get_backend # type: ignore |
16 | 17 |
|
17 | 18 | thismodule = sys.modules[__name__]
|
18 | 19 |
|
@@ -234,14 +235,25 @@ def __init__(
|
234 | 235 | self.ctrl = ctrl
|
235 | 236 |
|
236 | 237 | def __call__(self, *args: Any, **kws: Any) -> Gate:
|
| 238 | + # m = array_to_tensor(self.m) |
| 239 | + # m = backend.cast(m, dtypestr) |
237 | 240 | m = self.m.astype(npdtype)
|
238 | 241 | return Gate(deepcopy(m), name=self.n)
|
239 | 242 |
|
240 |
| - def adjoint(self, *args: Any, **kws: Any) -> "GateF": |
241 |
| - m = self.__call__(*args, **kws) |
242 |
| - ma = backend.adjoint(m.tensor) |
243 |
| - return GateF(ma, self.n + "d", self.ctrl) |
244 |
| - # TODO(@refraction-ray): adjoint gate convention finally determined |
| 243 | + def adjoint(self) -> "GateF": |
| 244 | + m = self.__call__() |
| 245 | + npb = get_backend("numpy") |
| 246 | + shape0 = npb.shape_tuple(m.tensor) |
| 247 | + m0 = npb.reshapem(m.tensor) |
| 248 | + ma = npb.adjoint(m0) |
| 249 | + if np.allclose(m0, ma, atol=1e-5): |
| 250 | + name = self.n |
| 251 | + else: |
| 252 | + name = self.n + "d" |
| 253 | + ma = npb.reshape(ma, shape0) |
| 254 | + return GateF(ma, name, self.ctrl) |
| 255 | + |
| 256 | + # TODO(@refraction-ray): adjoint gate convention finally determined |
245 | 257 |
|
246 | 258 | def controlled(self, *args: Any, **kws: Any) -> "GateF":
|
247 | 259 | def f(*args: Any, **kws: Any) -> Any:
|
@@ -304,6 +316,22 @@ def __init__(
|
304 | 316 | def __call__(self, *args: Any, **kws: Any) -> Gate:
|
305 | 317 | return self.f(*args, **kws)
|
306 | 318 |
|
| 319 | + def adjoint(self) -> "GateVF": |
| 320 | + def f(*args: Any, **kws: Any) -> Gate: |
| 321 | + m = self.__call__(*args, **kws) |
| 322 | + npb = get_backend("numpy") |
| 323 | + shape0 = npb.shape_tuple(m.tensor) |
| 324 | + m0 = npb.reshapem(m.tensor) |
| 325 | + ma = npb.adjoint(m0) |
| 326 | + if np.allclose(m0, ma, atol=1e-5): |
| 327 | + name = self.n |
| 328 | + else: |
| 329 | + name = self.n + "d" |
| 330 | + ma = npb.reshape(ma, shape0) |
| 331 | + return Gate(ma, name) |
| 332 | + |
| 333 | + return GateVF(f, self.n + "d", self.ctrl) |
| 334 | + |
307 | 335 |
|
308 | 336 | def meta_gate() -> None:
|
309 | 337 | """
|
|
0 commit comments