Skip to content

Commit

Permalink
syntax fix, added support for inp.integer, fixes #67
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesB-1qbit committed Dec 3, 2021
1 parent 340ddd7 commit 05b0b9f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tangelo/linq/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Gate(dict):
variational or not.
"""

<<<<<<< HEAD:tangelo/linq/gate.py
def __init__(self, name: str, target: Union[int, integer, list, ndarray],
control: Union[int, integer, list, ndarray] = None,
parameter="", is_variational: bool = False):
Expand All @@ -69,10 +70,31 @@ def check_qubit_indices(qubit_list, label):
target = (target.tolist() if isinstance(target, ndarray) else list(target)) if hasattr(target, "__iter__") else [target]

check_qubit_indices(target, "target")
=======
def __init__(self, name: str, target: Union[int, integer, list, ndarray], control: Union[int, integer, list, ndarray] = None,
parameter="", is_variational: bool = False):
""" This gate class is basically a dictionary with extra methods. """

if not isinstance(target, (int, integer, list, ndarray)):
raise ValueError("Qubit index must be int or list of ints.")
else:
if isinstance(target, (int, integer)):
target = [int(target)]
new_target = []
for t in target:
if isinstance(t, integer) and t >= 0:
new_target.append(int(t))
elif isinstance(t, int) and t >= 0:
new_target.append(t)
else:
raise ValueError(f"Target {t} of requested target={target} is not a positive integer")
target = new_target
>>>>>>> 6a87692 (syntax fix, added support for inp.integer, fixes #67):tangelo/backendbuddy/gate.py

if control is not None:
if name[0] != "C":
raise ValueError(f"Gate {name} was given control={control} but does not support controls. Try C{name}")
<<<<<<< HEAD:tangelo/linq/gate.py
control = (control.tolist() if isinstance(control, ndarray) else list(control)) if hasattr(control, "__iter__") else [control]
check_qubit_indices(control, "control")

Expand All @@ -82,6 +104,24 @@ def check_qubit_indices(qubit_list, label):
raise ValueError(f"There are duplicate qubits in the target/control qubits")

self.__dict__ = {"name": name, "target": target0, "target1": target1, "control": control,
=======
if not isinstance(control, (int, integer, list, ndarray)):
raise ValueError("Qubit index must be int or list of ints.")
else:
if isinstance(control, (int, integer)):
control = [int(control)]
new_control = []
for c in control:
if isinstance(c, integer) and c >= 0:
new_control.append(int(c))
elif isinstance(c, int) and c >= 0:
new_control.append(c)
else:
raise ValueError(f"Control {c} of requested control={control} is not a positive integer")
control = new_control

self.__dict__ = {"name": name, "target": target, "control": control,
>>>>>>> 6a87692 (syntax fix, added support for inp.integer, fixes #67):tangelo/backendbuddy/gate.py
"parameter": parameter, "is_variational": is_variational}

def __str__(self):
Expand Down
11 changes: 11 additions & 0 deletions tangelo/linq/tests/test_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ def test_some_gates(self):
RZ_gate = Gate("RZ", 1, parameter="an expression", is_variational=True)
# Create a multi-controlled X gate with a numpy array
CCCX_gate = Gate("CX", 0, control=np.array([1, 2, 4], dtype=np.int32))
<<<<<<< HEAD:tangelo/linq/tests/test_gates.py
# Create a gate with strings as inputs
CRZ_gate = Gate("CRZ", target=1, control=0, parameter=0.1)

for gate in [H_gate, CNOT_gate, RX_gate, RZ_gate, CCCX_gate, CRZ_gate]:
=======

for gate in [H_gate, CNOT_gate, RX_gate, RZ_gate, CCCX_gate]:
>>>>>>> 6a87692 (syntax fix, added support for inp.integer, fixes #67):tangelo/backendbuddy/tests/test_gates.py
print(gate)

def test_incorrect_gate(self):
Expand All @@ -51,10 +56,16 @@ def test_incorrect_gate(self):
self.assertRaises(ValueError, Gate, "H", -1)
self.assertRaises(ValueError, Gate, "CNOT", 0, control=0.3)
self.assertRaises(ValueError, Gate, 'X', target=0, control=1)
<<<<<<< HEAD:tangelo/linq/tests/test_gates.py
self.assertRaises(ValueError, Gate, "CNOT", target=0, control=0)

def test_integer_types(self):
""" Test to catch error with incorrect target or control qubit index type"""
=======

def test_integer_types(self):
""" Test to catch error with incorrect target or control"""
>>>>>>> 6a87692 (syntax fix, added support for inp.integer, fixes #67):tangelo/backendbuddy/tests/test_gates.py
self.assertRaises(ValueError, Gate, "CSWAP", target=[0, 'a'], control=np.array([1], dtype=np.int32))
self.assertRaises(ValueError, Gate, "X", target=0, control=[-1, 2, 3],)

Expand Down

0 comments on commit 05b0b9f

Please sign in to comment.