From 29b29d69820d9b2e56cfe5f7456ae0d8fb248e40 Mon Sep 17 00:00:00 2001 From: Mark C Date: Wed, 5 Jan 2022 21:35:24 +0000 Subject: [PATCH 1/3] feature: added controlled-sqrt-not gate This makes certain circuits, like CHSH, more straightforward. This commit works in line with the following branches (also committed, separately): https://github.com/unprovable/amazon-braket-schemas-python.git@ctrl-v-gate https://github.com/unprovable/amazon-braket-default-simulator-python.git@ctrl-v-gate --- src/braket/circuits/gates.py | 45 +++++++++++++++++++ test/unit_tests/braket/circuits/test_gates.py | 1 + tox.ini | 4 +- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/braket/circuits/gates.py b/src/braket/circuits/gates.py index 096d6488b..19e96482a 100644 --- a/src/braket/circuits/gates.py +++ b/src/braket/circuits/gates.py @@ -1046,6 +1046,51 @@ def cphaseshift10(control: QubitInput, target: QubitInput, angle: float) -> Inst 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 5d575b952..fbb35c6c1 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..00e4973e2 100644 --- a/tox.ini +++ b/tox.ini @@ -88,5 +88,5 @@ commands = [test-deps] 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/unprovable/amazon-braket-schemas-python.git@ctrl-v-gate + git+https://github.com/unprovable/amazon-braket-default-simulator-python.git@ctrl-v-gate From b702d2597682bafee7fd8fae959703d832fcbee3 Mon Sep 17 00:00:00 2001 From: Mark C Date: Mon, 24 Jan 2022 17:29:12 +0000 Subject: [PATCH 2/3] fix: ran tox linters --- src/braket/circuits/gates.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/braket/circuits/gates.py b/src/braket/circuits/gates.py index 19e96482a..f7c31af6e 100644 --- a/src/braket/circuits/gates.py +++ b/src/braket/circuits/gates.py @@ -1055,14 +1055,13 @@ def __init__(self): 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. + [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, ) @@ -1088,6 +1087,7 @@ def cv(control: QubitInput, target: QubitInput) -> Instruction: """ return Instruction(CV(), target=[control, target]) + Gate.register_gate(CV) From d64068e4442333223c3764dda0ce1572b6048984 Mon Sep 17 00:00:00 2001 From: Mark C Date: Wed, 26 Jan 2022 20:48:03 +0000 Subject: [PATCH 3/3] fix: reverted tox.ini --- tox.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index 00e4973e2..d6945f944 100644 --- a/tox.ini +++ b/tox.ini @@ -88,5 +88,5 @@ commands = [test-deps] deps = # If you need to test on a certain branch, add @ after .git - git+https://github.com/unprovable/amazon-braket-schemas-python.git@ctrl-v-gate - git+https://github.com/unprovable/amazon-braket-default-simulator-python.git@ctrl-v-gate + git+https://github.com/aws/amazon-braket-schemas-python.git + git+https://github.com/aws/amazon-braket-default-simulator-python.git \ No newline at end of file