Skip to content

Commit 855832d

Browse files
trying to add inverse U
1 parent 2f1ba36 commit 855832d

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

tensorcircuit/gates.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import sys
66
from copy import deepcopy
77
from functools import reduce, partial
8-
from typing import Any, Callable, Optional, Sequence, List, Union
8+
from typing import Any, Callable, Optional, Sequence, List, Union, Tuple
99
from operator import mul
1010

1111
import numpy as np
1212
import tensornetwork as tn
1313
from scipy.stats import unitary_group
1414

15-
from .cons import backend, dtypestr, npdtype
15+
from .cons import backend, dtypestr, npdtype, rdtypestr
1616
from .backends import get_backend # type: ignore
1717
from .utils import arg_alias
1818

@@ -466,6 +466,27 @@ def phase_gate(theta: float = 0) -> Gate:
466466
return Gate(unitary)
467467

468468

469+
# TODO(@refraction-ray): not correct now, correct impl required
470+
def get_u_parameter(m: Tensor) -> Tuple[Tensor, Tensor, Tensor]:
471+
"""
472+
From the single qubit unitary to infer three angles of IBMUgate
473+
474+
:param m: _description_
475+
:type m: Tensor
476+
:return: theta, phi, lbd
477+
:rtype: Tuple[Tensor, Tensor, Tensor]
478+
"""
479+
print("wrong impl, dont use!")
480+
a, b, c = m[0, 0], m[0, 1], m[1, 0]
481+
if a == 0:
482+
theta = np.pi
483+
else:
484+
theta = 2 * backend.atan(backend.sqrt(1 - a**2) / a)
485+
lbd = -1j * backend.log(b * backend.sqrt(1 - a**2) / (a**2 - 1))
486+
phi = -1j * backend.log(c / backend.sqrt(1 - a**2))
487+
return array_to_tensor(theta, phi, lbd, dtype=rdtypestr) # type: ignore
488+
489+
469490
def u_gate(theta: float = 0, phi: float = 0, lbd: float = 0) -> Gate:
470491
r"""
471492
IBMQ U gate following the converntion of OpenQASM3.0.

tensorcircuit/translation.py

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def qir2qiskit(qir: List[Dict[str, Any]], n: int) -> Any:
151151
)
152152
)
153153
qiskit_circ.unitary(qop, index[::-1], label=qis_name)
154+
# TODO(@refraction-ray): support for phase gate and U, cU gate for the circuit translation
154155
else: # r cr any gate
155156
qop = qi.Operator(
156157
np.reshape(

tests/test_gates.py

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import sys
22
import os
33
import numpy as np
4+
import pytest
5+
from pytest_lazyfixture import lazy_fixture as lf
46

57
thisfile = os.path.abspath(__file__)
68
modulepath = os.path.dirname(os.path.dirname(thisfile))
@@ -31,6 +33,18 @@ def test_cu_gate():
3133
np.testing.assert_allclose(m[:2, :2], np.eye(2), atol=1e-5)
3234

3335

36+
@pytest.mark.skip(reason="to be implemented")
37+
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb")])
38+
def test_get_u_parameter(backend, highp):
39+
for _ in range(6):
40+
hermitian = np.random.uniform(size=[2, 2])
41+
hermitian += np.conj(np.transpose(hermitian))
42+
unitary = tc.backend.expm(hermitian * 1.0j)
43+
params = tc.gates.get_u_parameter(unitary)
44+
unitary2 = tc.gates.u_gate(theta=params[0], phi=params[1], lbd=params[2])
45+
np.testing.assert_allclose(unitary, unitary2.tensor, atol=1e-3)
46+
47+
3448
def test_exp_gate():
3549
c = tc.Circuit(2)
3650
c.exp(

0 commit comments

Comments
 (0)