Skip to content

Commit

Permalink
Change barrier/delay default behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjaskula-aws committed Aug 11, 2023
1 parent f62ea2f commit cda4f70
Showing 2 changed files with 49 additions and 7 deletions.
24 changes: 18 additions & 6 deletions oqpy/program.py
Original file line number Diff line number Diff line change
@@ -384,19 +384,31 @@ def declare(
return self

def delay(
self, time: AstConvertible, qubits_or_frames: AstConvertible | Iterable[AstConvertible] = ()
self,
time: AstConvertible,
qubits_or_frames: AstConvertible | Iterable[AstConvertible] | None = None,
) -> Program:
"""Apply a delay to a set of qubits or frames."""
if not isinstance(qubits_or_frames, Iterable):
qubits_or_frames = [qubits_or_frames]
if isinstance(qubits_or_frames, Iterable) and not any(True for _ in qubits_or_frames):
return self
elif qubits_or_frames is None:
ast_qubits_or_frames = []
else:
if not isinstance(qubits_or_frames, Iterable):
qubits_or_frames = [qubits_or_frames]
ast_qubits_or_frames = map_to_ast(self, qubits_or_frames)
ast_duration = to_ast(self, convert_float_to_duration(time))
ast_qubits_or_frames = map_to_ast(self, qubits_or_frames)
self._add_statement(ast.DelayInstruction(ast_duration, ast_qubits_or_frames))
return self

def barrier(self, qubits_or_frames: Iterable[AstConvertible]) -> Program:
def barrier(self, qubits_or_frames: Iterable[AstConvertible] | None = None) -> Program:
"""Apply a barrier to a set of qubits or frames."""
ast_qubits_or_frames = map_to_ast(self, qubits_or_frames)
if isinstance(qubits_or_frames, Iterable) and not any(True for _ in qubits_or_frames):
return self
elif qubits_or_frames is None:
ast_qubits_or_frames = []
else:
ast_qubits_or_frames = map_to_ast(self, qubits_or_frames)
self._add_statement(ast.QuantumBarrier(ast_qubits_or_frames))
return self

32 changes: 31 additions & 1 deletion tests/test_directives.py
Original file line number Diff line number Diff line change
@@ -862,6 +862,36 @@ def multiply(int[32] x, int[32] y) -> int[32] {
_check_respects_type_hints(prog)


def test_barrier_delay_arguments():
port = PortVar("portname")
frame = FrameVar(port, 1e9, name="frame0")
frame1 = FrameVar(port, 1.5e9, name="frame1")
prog = Program()
prog.barrier()
prog.delay(1e-7)
prog.barrier([])
prog.delay(2e-7, [])
prog.barrier([frame, frame1])
prog.delay(3e-7, frame1)
prog.delay(4e-7, [frame, frame1])

expected = textwrap.dedent(
"""
OPENQASM 3.0;
port portname;
frame frame0 = newframe(portname, 1000000000.0, 0);
frame frame1 = newframe(portname, 1500000000.0, 0);
barrier;
delay[100.0ns];
barrier frame0, frame1;
delay[300.0ns] frame1;
delay[400.0ns] frame0, frame1;
"""
).strip()

assert prog.to_qasm() == expected


def test_box_and_timings():
constant = declare_waveform_generator("constant", [("length", duration), ("iq", complex128)])

@@ -1501,8 +1531,8 @@ def _to_oqpy_expression(self):
"""
OPENQASM 3.0;
duration a1 = 100.0ns;
duration a2 = 100.0ns;
frame f1;
duration a2 = 100.0ns;
a1 = 2;
delay[a2] f1;
"""

0 comments on commit cda4f70

Please sign in to comment.