diff --git a/src/braket/circuits/gates.py b/src/braket/circuits/gates.py index 9d4f3aa74..651b59c1c 100644 --- a/src/braket/circuits/gates.py +++ b/src/braket/circuits/gates.py @@ -1237,6 +1237,51 @@ def cphaseshift10( Gate.register_gate(CPhaseShift10) +class CV(Gate): + """Controlled Sqrt of NOT gate.""" + + def __init__(self): + super().__init__(qubit_count=None, ascii_symbols=["C", "V"]) + + def to_ir(self, target: QubitSet): + return ir.CV.construct(control=target[0], target=target[1]) + + def to_matrix(self) -> np.ndarray: + return np.array( + [ + [1.0, 0.0, 0.0, 0.0], + [0.0, 1.0, 0.0, 0.0], + [0.0, 0.0, 0.5 + 0.5j, 0.5 - 0.5j], # if the control bit, then apply the V gate + [0.0, 0.0, 0.5 - 0.5j, 0.5 + 0.5j], # which is the sqrt(NOT) gate. + ], + dtype=complex, + ) + + @staticmethod + def fixed_qubit_count() -> int: + return 2 + + @staticmethod + @circuit.subroutine(register=True) + def cv(control: QubitInput, target: QubitInput) -> Instruction: + """Registers this function into the circuit class. + + Args: + control (Qubit or int): Control qubit index. + target (Qubit or int): Target qubit index. + + Returns: + Instruction: CV instruction. + + Examples: + >>> circ = Circuit().cv(0, 1) + """ + return Instruction(CV(), target=[control, target]) + + +Gate.register_gate(CV) + + class CY(Gate): """Controlled Pauli-Y gate.""" diff --git a/test/unit_tests/braket/circuits/test_gates.py b/test/unit_tests/braket/circuits/test_gates.py index f1be74968..475456cd4 100644 --- a/test/unit_tests/braket/circuits/test_gates.py +++ b/test/unit_tests/braket/circuits/test_gates.py @@ -42,6 +42,7 @@ (Gate.Ry, "ry", ir.Ry, [SingleTarget, Angle], {}), (Gate.Rz, "rz", ir.Rz, [SingleTarget, Angle], {}), (Gate.CNot, "cnot", ir.CNot, [SingleTarget, SingleControl], {}), + (Gate.CV, "cv", ir.CV, [SingleTarget, SingleControl], {}), (Gate.CCNot, "ccnot", ir.CCNot, [SingleTarget, DoubleControl], {}), (Gate.Swap, "swap", ir.Swap, [DoubleTarget], {}), (Gate.CSwap, "cswap", ir.CSwap, [SingleControl, DoubleTarget], {}), diff --git a/tox.ini b/tox.ini index 1ab05fcc9..d6945f944 100644 --- a/tox.ini +++ b/tox.ini @@ -89,4 +89,4 @@ commands = deps = # If you need to test on a certain branch, add @ after .git git+https://github.com/aws/amazon-braket-schemas-python.git - git+https://github.com/aws/amazon-braket-default-simulator-python.git + git+https://github.com/aws/amazon-braket-default-simulator-python.git \ No newline at end of file