Skip to content

Commit

Permalink
feature: added controlled-sqrt-not gate (#297)
Browse files Browse the repository at this point in the history
* 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

* fix: ran tox linters

* fix: reverted tox.ini

Co-authored-by: Mark C <[email protected]>
Co-authored-by: Mark C <[email protected]>
Co-authored-by: Cody Wang <[email protected]>
  • Loading branch information
4 people authored and krneta committed Jan 27, 2022
1 parent 03d146d commit f316017
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/braket/circuits/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
1 change: 1 addition & 0 deletions test/unit_tests/braket/circuits/test_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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], {}),
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ commands =
deps =
# If you need to test on a certain branch, add @<branch-name> 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

0 comments on commit f316017

Please sign in to comment.