Skip to content

Commit a541c19

Browse files
add su4 gate
1 parent 14fcdb0 commit a541c19

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
- Add exp and expm for torch backend.
1212

13+
- Add `su4` as a generic parameterized two-qubit gates.
14+
1315
## v1.4.0
1416

1517
### Added

tensorcircuit/abstractcircuit.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"any",
5454
"exp",
5555
"exp1",
56+
"su4",
5657
]
5758
mpogates = ["multicontrol", "mpo"]
5859
gate_aliases = [

tensorcircuit/gates.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,43 @@ def exponential_gate_unity(
870870
return Gate(mat, name="exp1-" + name)
871871

872872

873+
def su4_gate(theta: Tensor, name: str = "su(4)") -> Gate:
874+
r"""
875+
Two-qubit general SU(4) gate.
876+
877+
:param theta: the angle tensor (15 components) of the gate.
878+
:type theta: Tensor
879+
:param name: the name of the gate.
880+
:type name: str
881+
:return: a gate object.
882+
:rtype: Gate
883+
"""
884+
theta = num_to_tensor(theta)
885+
pauli_ops = array_to_tensor(
886+
_ix_matrix,
887+
_iy_matrix,
888+
_iz_matrix,
889+
_xi_matrix,
890+
_xx_matrix,
891+
_xy_matrix,
892+
_xz_matrix,
893+
_yi_matrix,
894+
_yx_matrix,
895+
_yy_matrix,
896+
_yz_matrix,
897+
_zi_matrix,
898+
_zx_matrix,
899+
_zy_matrix,
900+
_zz_matrix,
901+
)
902+
generator = backend.sum(
903+
backend.stack([theta[i] * pauli_ops[i] for i in range(15)]), axis=0
904+
)
905+
mat = backend.expm(-1j * generator)
906+
mat = backend.reshape2(mat)
907+
return Gate(mat, name=name)
908+
909+
873910
exp1_gate = exponential_gate_unity
874911
# exp1 = exponential_gate_unity
875912
rzz_gate = partial(exp1_gate, unitary=_zz_matrix, half=True)
@@ -974,6 +1011,7 @@ def meta_vgate() -> None:
9741011
"rzz",
9751012
"rxx",
9761013
"ryy",
1014+
"su4",
9771015
]:
9781016
for funcname in [f, f + "gate"]:
9791017
setattr(thismodule, funcname, GateVF(getattr(thismodule, f + "_gate"), f))

tests/test_circuit.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ def test_expectation(backend):
260260
)
261261

262262

263+
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb"), lf("torchb")])
264+
def test_su4gate(backend, highp):
265+
def f(param):
266+
c = tc.Circuit(8)
267+
for d in range(6):
268+
for i in range(8):
269+
c.su4(i, (i + 1) % 8, theta=param[d, i])
270+
return tc.backend.norm(c.state())
271+
272+
np.testing.assert_allclose(f(tc.backend.ones([6, 8, 15])), 1, atol=1e-7)
273+
274+
263275
@pytest.mark.parametrize("backend", [lf("npb"), lf("tfb"), lf("jaxb"), lf("cpb")])
264276
def test_exp1(backend):
265277
@partial(tc.backend.jit, jit_compile=True)

0 commit comments

Comments
 (0)