From 4589846d757bfd0c650435c35b2b3209248f1aad Mon Sep 17 00:00:00 2001 From: GenericP3rson Date: Thu, 20 Jul 2023 12:39:52 -0400 Subject: [PATCH] Added C4X Gate --- test/operators/test_op.py | 1 + torchquantum/functional.py | 68 ++++++++++++++++++++++++++++++++++++++ torchquantum/operators.py | 14 ++++++++ 3 files changed, 83 insertions(+) diff --git a/test/operators/test_op.py b/test/operators/test_op.py index c0fd2eac..568e69b8 100644 --- a/test/operators/test_op.py +++ b/test/operators/test_op.py @@ -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}, diff --git a/torchquantum/functional.py b/torchquantum/functional.py index 94c77074..3bebcadc 100644 --- a/torchquantum/functional.py +++ b/torchquantum/functional.py @@ -36,6 +36,7 @@ "sswap", "cswap", "toffoli", + "c4x", "multicnot", "multixcnot", "rx", @@ -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( @@ -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 ) @@ -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, @@ -3270,6 +3337,7 @@ def ecr( "sswap": sswap, "cswap": cswap, "toffoli": toffoli, + "c4x": c4x, "phaseshift": phaseshift, "p": p, "cp": cp, diff --git a/torchquantum/operators.py b/torchquantum/operators.py index 42d5de04..4df6bf90 100644 --- a/torchquantum/operators.py +++ b/torchquantum/operators.py @@ -43,6 +43,7 @@ "SSWAP", "CSWAP", "Toffoli", + "C4X", "PhaseShift", "Rot", "MultiRZ", @@ -118,6 +119,7 @@ class Operator(tq.QuantumModule): "SSWAP", "CSWAP", "Toffoli", + "C4X", "MultiCNOT", "MultiXCNOT", "Reset", @@ -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.""" @@ -1358,6 +1371,7 @@ def _matrix(cls, params): "cswap": CSWAP, "toffoli": Toffoli, "ccx": Toffoli, + "c4x": C4X, "phaseshift": PhaseShift, "rot": Rot, "multirz": MultiRZ,