Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoQASM freeparameter usage with pulse instruction #794

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/braket/experimental/autoqasm/pulse/pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def _pulse_instruction(name: str, frame: Frame, *args) -> None:
"""
program_conversion_context = aq_program.get_program_conversion_context()
program_conversion_context._has_pulse_control = True
program_conversion_context.register_args(args)
Copy link
Contributor

@laurencap laurencap Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need to be replaced with the new _register_and_convert_parameters method of utils. I'm guessing it would only appear as a bug if a FreeParameter appeared in a pulse instruction first, and later was used in a conditional statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditional statements later in a program would not be affected by here. In conditional statement, variables are still converted by _register_and_convert_parameters in the compare operator. I added a unit test that the freeparameter for pulse is later used by a conditional statement. Here, we actually do want the variable to be freeparameter instead of FloatVar.


pulse_sequence = PulseSequence()
pulse_sequence._program = program_conversion_context.get_oqpy_program(
Expand Down
71 changes: 71 additions & 0 deletions test/unit_tests/braket/experimental/autoqasm/test_pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
shift_frequency,
shift_phase,
)
from braket.parametric import FreeParameter
from braket.pulse import ArbitraryWaveform, Frame, Port

PORT = Port(port_id="device_port_x0", dt=1e-9, properties={})
Expand Down Expand Up @@ -183,3 +184,73 @@ def test_pulse_control_invalid_physical_qubit(instruction, qubits_or_frames, par
with pytest.raises(ValueError):
with aq.build_program():
instruction(qubits_or_frames, *params)


def test_pulse_freeparameter() -> None:
"""Test pulse program with freeparameter."""

@aq.main
def my_program():
delay(["$3", "$4"], FreeParameter("duration"))

expected = textwrap.dedent(
"""
OPENQASM 3.0;
input float[64] duration;
laurencap marked this conversation as resolved.
Show resolved Hide resolved
cal {
delay[(duration) * 1s] $3, $4;
}
"""
).strip()
qasm = my_program().to_ir()
assert qasm == expected


def test_pulse_freeparameter_bound() -> None:
"""Test pulse program with freeparameter bound with values."""

@aq.main
def my_program():
delay(["$3", "$4"], FreeParameter("duration"))

expected = textwrap.dedent(
"""
OPENQASM 3.0;
float[64] duration = 0.123;
cal {
delay[(duration) * 1s] $3, $4;
}
"""
).strip()
qasm = my_program().make_bound_program({"duration": 0.123}).to_ir()
assert qasm == expected


def test_pulse_freeparameter_condition() -> None:
"""Test pulse program with freeparameter in condition."""

@aq.main
def my_program():
delay(["$3", "$4"], FreeParameter("duration"))
if FreeParameter("duration") > FreeParameter("duration2"):
delay(["$3", "$4"], FreeParameter("duration2"))

expected = textwrap.dedent(
"""
OPENQASM 3.0;
input float[64] duration;
input float[64] duration2;
cal {
delay[(duration) * 1s] $3, $4;
}
bool __bool_0__;
__bool_0__ = duration > duration2;
if (__bool_0__) {
cal {
delay[(duration2) * 1s] $3, $4;
}
}
"""
).strip()
qasm = my_program().to_ir()
assert qasm == expected