Skip to content

Commit

Permalink
Added C4X Gate
Browse files Browse the repository at this point in the history
  • Loading branch information
01110011011101010110010001101111 committed Jul 20, 2023
1 parent ce1eca8 commit 4589846
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/operators/test_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# {'qiskit': qiskit_gate.?, 'tq': tq.SSWAP},
{"qiskit": qiskit_gate.CSwapGate, "tq": tq.CSWAP},
{"qiskit": qiskit_gate.CCXGate, "tq": tq.Toffoli},
{"qiskit": qiskit_gate.C4XGate, "tq": tq.C4X},
{"qiskit": qiskit_gate.PhaseGate, "tq": tq.PhaseShift},
# {'qiskit': qiskit_gate.?, 'tq': tq.Rot},
# {'qiskit': qiskit_gate.?, 'tq': tq.MultiRZ},
Expand Down
68 changes: 68 additions & 0 deletions torchquantum/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"sswap",
"cswap",
"toffoli",
"c4x",
"multicnot",
"multixcnot",
"rx",
Expand Down Expand Up @@ -1100,6 +1101,24 @@ def singleexcitation_matrix(params):

return matrix.squeeze(0)

def c4x_matrix():
"""Compute unitary matrix for C4X gate.
Args:
None
Returns:
torch.Tensor: The computed unitary matrix.
"""
mat = torch.eye(32, dtype=C_DTYPE)
mat[30][30] = 0
mat[30][31] = 1
mat[31][31] = 0
mat[31][30] = 1

return mat


mat_dict = {
"hadamard": torch.tensor(
Expand Down Expand Up @@ -1166,6 +1185,7 @@ def singleexcitation_matrix(params):
],
dtype=C_DTYPE,
),
"c4x": c4x_matrix(),
"ecr": torch.tensor(
[[0, 0, 1, 1j], [0, 0, 1j, 1], [1, -1j, 0, 0], [-1j, 1, 0, 0]], dtype=C_DTYPE
)
Expand Down Expand Up @@ -2280,6 +2300,53 @@ def toffoli(
)


def c4x(
q_device,
wires,
params=None,
n_wires=None,
static=False,
parent_graph=None,
inverse=False,
comp_method="bmm",
):
"""Perform the c4x gate.
Args:
q_device (tq.QuantumDevice): The QuantumDevice.
wires (Union[List[int], int]): Which qubit(s) to apply the gate.
params (torch.Tensor, optional): Parameters (if any) of the gate.
Default to None.
n_wires (int, optional): Number of qubits the gate is applied to.
Default to None.
static (bool, optional): Whether use static mode computation.
Default to False.
parent_graph (tq.QuantumGraph, optional): Parent QuantumGraph of
current operation. Default to None.
inverse (bool, optional): Whether inverse the gate. Default to False.
comp_method (bool, optional): Use 'bmm' or 'einsum' method to perform
matrix vector multiplication. Default to 'bmm'.
Returns:
None.
"""
name = "c4x"
mat = mat_dict[name]
gate_wrapper(
name=name,
mat=mat,
method=comp_method,
q_device=q_device,
wires=wires,
params=params,
n_wires=n_wires,
static=static,
parent_graph=parent_graph,
inverse=inverse,
)


def phaseshift(
q_device,
wires,
Expand Down Expand Up @@ -3270,6 +3337,7 @@ def ecr(
"sswap": sswap,
"cswap": cswap,
"toffoli": toffoli,
"c4x": c4x,
"phaseshift": phaseshift,
"p": p,
"cp": cp,
Expand Down
14 changes: 14 additions & 0 deletions torchquantum/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"SSWAP",
"CSWAP",
"Toffoli",
"C4X",
"PhaseShift",
"Rot",
"MultiRZ",
Expand Down Expand Up @@ -118,6 +119,7 @@ class Operator(tq.QuantumModule):
"SSWAP",
"CSWAP",
"Toffoli",
"C4X",
"MultiCNOT",
"MultiXCNOT",
"Reset",
Expand Down Expand Up @@ -809,6 +811,17 @@ class Toffoli(Operation, metaclass=ABCMeta):
def _matrix(cls, params):
return cls.matrix

class C4X(Operation, metaclass=ABCMeta):
"""Class for C4X Gate."""

num_params = 0
num_wires = 5
matrix = mat_dict["c4x"]
func = staticmethod(tqf.c4x)

@classmethod
def _matrix(cls, params):
return cls.matrix

class RX(Operation, metaclass=ABCMeta):
"""Class for RX Gate."""
Expand Down Expand Up @@ -1358,6 +1371,7 @@ def _matrix(cls, params):
"cswap": CSWAP,
"toffoli": Toffoli,
"ccx": Toffoli,
"c4x": C4X,
"phaseshift": PhaseShift,
"rot": Rot,
"multirz": MultiRZ,
Expand Down

0 comments on commit 4589846

Please sign in to comment.