Skip to content

Commit 2f1ba36

Browse files
add qasm U gate support
1 parent 4fe74c1 commit 2f1ba36

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

examples/tcgates.inc

+1-5
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,4 @@ gate rzz(θ) a, b { cx a, b; rz(θ) b; cx a, b; }
110110
gate iswap(θ) a, b {cx a, b; h a; cx b, a; phase(θ*π/2) a; cx b, a; phase(-θ*π/2) a; h a; cx a,b; }
111111

112112
// wroot
113-
gate wroot a {U(π/2, -π/4, π/4) a; }
114-
115-
// to be determined
116-
// gate r()
117-
// gate cr() a, b { ctrl @ r() a, b; }
113+
gate wroot a { U(π/2, -π/4, π/4) a; }

tensorcircuit/abstractcircuit.py

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
vgates = [
2929
"r",
3030
"cr",
31+
"u",
32+
"cu",
3133
"rx",
3234
"ry",
3335
"rz",

tensorcircuit/experimental.py

-13
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,3 @@ def grad_f(*args: Any, **kws: Any) -> Any:
258258
return grad_values[0]
259259

260260
return grad_f
261-
262-
263-
def IBMUgate(theta: float, phi: float, lbd: float) -> Tensor:
264-
# not jittable: just for correctness check
265-
return np.array(
266-
[
267-
[np.cos(theta / 2), -np.exp(1.0j * lbd) * np.sin(theta / 2)],
268-
[
269-
np.exp(1.0j * phi) * np.sin(theta / 2),
270-
np.exp(1.0j * (phi + lbd)) * np.cos(theta / 2),
271-
],
272-
]
273-
)

tensorcircuit/gates.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,41 @@ def phase_gate(theta: float = 0) -> Gate:
466466
return Gate(unitary)
467467

468468

469+
def u_gate(theta: float = 0, phi: float = 0, lbd: float = 0) -> Gate:
470+
r"""
471+
IBMQ U gate following the converntion of OpenQASM3.0.
472+
See `OpenQASM doc <https://openqasm.com/language/gates.html#built-in-gates>`_
473+
474+
.. math::
475+
476+
\begin{split}U(\theta,\phi,\lambda) := \left(\begin{array}{cc}
477+
\cos(\theta/2) & -e^{i\lambda}\sin(\theta/2) \\
478+
e^{i\phi}\sin(\theta/2) & e^{i(\phi+\lambda)}\cos(\theta/2) \end{array}\right).\end{split}
479+
480+
:param theta: _description_, defaults to 0
481+
:type theta: float, optional
482+
:param phi: _description_, defaults to 0
483+
:type phi: float, optional
484+
:param lbd: _description_, defaults to 0
485+
:type lbd: float, optional
486+
:return: _description_
487+
:rtype: Gate
488+
"""
489+
i00, i01, i10, i11 = array_to_tensor(
490+
np.array([[1, 0], [0, 0]]),
491+
np.array([[0, 1], [0, 0]]),
492+
np.array([[0, 0], [1, 0]]),
493+
np.array([[0, 0], [0, 1]]),
494+
)
495+
unitary = (
496+
backend.cos(theta / 2) * i00
497+
- backend.exp(1.0j * lbd) * backend.sin(theta / 2) * i01
498+
+ backend.exp(1.0j * phi) * backend.sin(theta / 2) * i10
499+
+ backend.exp(1.0j * (phi + lbd)) * backend.cos(theta / 2) * i11
500+
)
501+
return Gate(unitary)
502+
503+
469504
def r_gate(theta: float = 0, alpha: float = 0, phi: float = 0) -> Gate:
470505
r"""
471506
General single qubit rotation gate
@@ -871,6 +906,7 @@ def mpo_gate(mpo: Operator, name: str = "mpo") -> Operator:
871906
def meta_vgate() -> None:
872907
for f in [
873908
"r",
909+
"u",
874910
"rx",
875911
"ry",
876912
"rz",
@@ -886,7 +922,7 @@ def meta_vgate() -> None:
886922
]:
887923
for funcname in [f, f + "gate", f + "_gate"]:
888924
setattr(thismodule, funcname, GateVF(getattr(thismodule, f + "_gate"), f))
889-
for f in ["crx", "cry", "crz"]:
925+
for f in ["cu", "crx", "cry", "crz"]:
890926
for funcname in [f, f + "gate", f + "_gate"]:
891927
setattr(thismodule, funcname, getattr(thismodule, f[1:]).controlled())
892928
for f in ["ox", "oy", "oz", "orx", "ory", "orz"]:

tests/test_gates.py

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ def test_phase_gate():
2222
np.testing.assert_allclose(c.state()[1], 0.7071j, atol=1e-4)
2323

2424

25+
def test_cu_gate():
26+
c = tc.Circuit(2)
27+
c.cu(0, 1, theta=np.pi / 2, phi=-np.pi / 4, lbd=np.pi / 4)
28+
m = c.matrix()
29+
print(m)
30+
np.testing.assert_allclose(m[2:, 2:], tc.gates._wroot_matrix, atol=1e-5)
31+
np.testing.assert_allclose(m[:2, :2], np.eye(2), atol=1e-5)
32+
33+
2534
def test_exp_gate():
2635
c = tc.Circuit(2)
2736
c.exp(

0 commit comments

Comments
 (0)