Skip to content

Commit

Permalink
braket custom control fix #102
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhill1 committed Aug 28, 2022
1 parent 8ba405f commit 3b96a00
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion qbraid/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
Version number (major.minor.patch[-label])
"""
__version__ = "0.1.3.dev4"
__version__ = "0.1.3.dev5"
42 changes: 23 additions & 19 deletions qbraid/transpiler/cirq_braket/custom_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
"""
import itertools
from typing import Any, List

import braket.ir.jaqcd as ir
import numpy as np
from braket.circuits import Gate, Instruction, circuit
from braket.circuits.qubit_set import QubitSet
from braket.circuits import Gate, Instruction, QubitSet, circuit
from braket.circuits.gates import Unitary, format_complex
from braket.circuits.serialization import OpenQASMSerializationProperties

# pylint: disable=missing-function-docstring


class C(Gate):
Expand All @@ -31,7 +35,7 @@ def _extend_matrix(self, sub_matrix: np.ndarray) -> np.ndarray:
qid_shape = (2,) * self.qubit_count
control_values = ((1,),) * self._num_controls
sub_n = len(qid_shape) - self._num_controls
tensor = np.eye(np.prod(qid_shape, dtype=np.int64).item(), dtype=sub_matrix.dtype)
tensor = np.eye(np.prod(qid_shape, dtype=np.int64).item(), dtype=complex)
tensor.shape = qid_shape * 2
sub_tensor = sub_matrix.reshape(qid_shape[self._num_controls :] * 2)
for control_vals in itertools.product(*control_values):
Expand All @@ -48,27 +52,27 @@ def to_matrix(self, *args, **kwargs) -> np.ndarray: # pylint: disable=unused-ar
sub_matrix = self.sub_gate.to_matrix()
return self._extend_matrix(sub_matrix)

def to_ir(self, target: QubitSet):
"""Returns IR object of quantum operator and target
def adjoint(self) -> List[Gate]:
return [Unitary(self.to_matrix().conj().T, display_name=f"({self.ascii_symbols})^†")]

Args:
target (QubitSet): target qubit(s)
Returns:
IR object of the quantum operator and target
"""
def _to_jaqcd(self, target: QubitSet) -> Any:
return ir.Unitary.construct(
targets=list(target),
matrix=C._transform_matrix_to_ir(self.to_matrix()),
matrix=Unitary._transform_matrix_to_ir(self.to_matrix()),
)

def __eq__(self, other):
if isinstance(other, C):
return self.matrix_equivalence(other)
return NotImplemented

@staticmethod
def _transform_matrix_to_ir(matrix: np.ndarray):
return [[[element.real, element.imag] for element in row] for row in matrix.tolist()]
def _to_openqasm( # pylint: disable=unused-argument
self, target: QubitSet, serialization_properties: OpenQASMSerializationProperties, **kwargs
) -> str:
qubits = [serialization_properties.format_target(int(qubit)) for qubit in target]
formatted_matrix = np.array2string(
self.to_matrix(),
separator=", ",
formatter={"all": lambda x: format_complex(x)}, # pylint: disable=unnecessary-lambda
threshold=float("inf"),
).replace("\n", "")

return f"#pragma braket unitary({formatted_matrix}) {', '.join(qubits)}"

@staticmethod
@circuit.subroutine(register=True)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ numpy==1.23.2
PennyLane==0.25.1
PennyLane-qiskit==0.24.0
pyquil==3.2.1
qiskit==0.37.1
qiskit==0.37.2
requests==2.28.1
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ install_requires =
PennyLane==0.25.1
PennyLane-qiskit==0.24.0
pyquil==3.2.1
qiskit==0.37.1
qiskit==0.37.2
requests==2.28.1
ipython

Expand Down

0 comments on commit 3b96a00

Please sign in to comment.