Skip to content

Commit 62f2ce3

Browse files
authored
Merge pull request #1332 from qiboteam/align
Align as parametrized gate
2 parents aa28374 + e109b89 commit 62f2ce3

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/qibo/backends/npmatrices.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def I(self, n=2):
6464
# _cast will take care of casting in the right dtype for all the backends
6565
return self._cast(self.np.eye(n, dtype=complex), dtype=self.dtype)
6666

67-
def Align(self, n=2):
67+
def Align(self, delay, n=2):
6868
return self._cast(self.I(n), dtype=self.dtype)
6969

7070
def M(self): # pragma: no cover

src/qibo/gates/gates.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -503,30 +503,31 @@ def qasm_label(self):
503503
return "id"
504504

505505

506-
class Align(Gate):
506+
class Align(ParametrizedGate):
507507
"""Aligns proceeding qubit operations and (optionally) waits ``delay`` amount of time.
508508
509509
Args:
510-
*q (int): The qubit ID numbers.
510+
q (int): The qubit ID.
511511
delay (int, optional): The time (in ns) for which to delay circuit execution on the specified qubits.
512512
Defaults to ``0`` (zero).
513513
"""
514514

515-
def __init__(self, *q, delay: int = 0):
515+
def __init__(self, q, delay=0, trainable=True):
516516
if not isinstance(delay, int):
517517
raise_error(
518518
TypeError, f"delay must be type int, but it is type {type(delay)}."
519519
)
520520
if delay < 0.0:
521521
raise_error(ValueError, "Delay must not be negative.")
522522

523-
super().__init__()
523+
super().__init__(trainable)
524524
self.name = "align"
525-
self.delay = delay
526525
self.draw_label = f"A({delay})"
527-
self.init_args = q
528-
self.init_kwargs = {"delay": delay}
529-
self.target_qubits = tuple(q)
526+
self.init_args = [q]
527+
self.init_kwargs = {"name": self.name, "delay": delay, "trainable": trainable}
528+
self.target_qubits = (q,)
529+
self._parameters = (delay,)
530+
self.nparams = 1
530531

531532

532533
def _is_clifford_given_angle(angle):

tests/test_gates_gates.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ def test_align(backend):
212212
with pytest.raises(ValueError):
213213
gates.Align(0, delay=-1)
214214

215-
nqubits = 2
215+
nqubits = 1
216216

217-
gate = gates.Align(0, 1)
218-
gate_list = [gates.H(0), gates.H(1), gate]
217+
gate = gates.Align(0, 0)
218+
gate_list = [gates.H(0), gate]
219219

220-
final_state = apply_gates(backend, gate_list, nqubits=2)
220+
final_state = apply_gates(backend, gate_list, nqubits=nqubits)
221221
target_state = backend.plus_state(nqubits)
222222

223223
backend.assert_allclose(final_state, target_state)
@@ -229,8 +229,8 @@ def test_align(backend):
229229
with pytest.raises(NotImplementedError):
230230
gate.qasm_label
231231

232-
assert not gates.Align(0, 1).clifford
233-
assert not gates.Align(0, 1).unitary
232+
assert not gates.Align(0, delay=0).clifford
233+
assert not gates.Align(0, delay=0).unitary
234234

235235

236236
# :class:`qibo.core.cgates.M` is tested seperately in `test_measurement_gate.py`

0 commit comments

Comments
 (0)