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

feature: Add get_compiled_circuit convenience method #787

Merged
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
17 changes: 17 additions & 0 deletions src/braket/tasks/gate_model_quantum_task_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@ def __eq__(self, other) -> bool:
return self.task_metadata.id == other.task_metadata.id
return NotImplemented

def get_compiled_circuit(self) -> Optional[str]:
"""
Get the compiled circuit, if one is available.

Returns:
Optional[str]: The compiled circuit or None.
"""
metadata = self.additional_metadata
if not metadata:
return None
if metadata.rigettiMetadata:
return metadata.rigettiMetadata.compiledProgram
elif metadata.oqcMetadata:
return metadata.oqcMetadata.compiledProgram
else:
return None

@staticmethod
def measurement_counts_from_measurements(measurements: np.ndarray) -> Counter:
"""
Expand Down
112 changes: 112 additions & 0 deletions test/unit_tests/braket/tasks/test_gate_model_quantum_task_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
ResultTypeValue,
TaskMetadata,
)
from braket.task_result.oqc_metadata_v1 import OqcMetadata
from braket.task_result.rigetti_metadata_v1 import RigettiMetadata
from braket.tasks import GateModelQuantumTaskResult


Expand Down Expand Up @@ -61,6 +63,79 @@ def additional_metadata_openqasm():
return AdditionalMetadata(action=program)


@pytest.fixture
def quil_program():
return """
PRAGMA INITIAL_REWIRING "NAIVE"
RESET
DECLARE ro BIT[2]
PRAGMA PRESERVE_BLOCK
RX(1.5707963267948966) 0
RX(1.5707963267948966) 7
RZ(1.5707963267948966) 0
RZ(1.5707963267948966) 7
RX(-1.5707963267948966) 7
CZ 0 7
RZ(3.141592653589793) 7
RX(1.5707963267948966) 7
RZ(1.5707963267948966) 7
RX(-1.5707963267948966) 7
PRAGMA END_PRESERVE_BLOCK
MEASURE 0 ro[0]
MEASURE 7 ro[1]
"""


@pytest.fixture
def additional_metadata_rigetti(quil_program):
program = openqasm.Program(
source="""
OPENQASM 3.0;
bit[2] b;
h $0;
cnot $0, $7;
b[0] = measure $0;
b[1] = measure $7;
"""
)
rigetti_metadata = RigettiMetadata(compiledProgram=quil_program)

return AdditionalMetadata(action=program, rigettiMetadata=rigetti_metadata)


@pytest.fixture
def qasm2_program():
return """
OPENQASM 2.0;
include "qelib1.inc";

qreg node[8];
creg b[2];
u3(0.5*pi,0.0*pi,1.0*pi) node[0];
cx node[0],node[1];
measure node[0] -> b[0];
measure node[1] -> b[1];
"""


@pytest.fixture
def additional_metadata_oqc(qasm2_program):
program = openqasm.Program(
source="""
OPENQASM 3.0;
bit[2] b;
qubit[8] q;
h q[0];
cnot q[0], q[7];
b[0] = measure q[0];
b[1] = measure q[7];
"""
)
oqc_metadata = OqcMetadata(compiledProgram=qasm2_program)

return AdditionalMetadata(action=program, oqcMetadata=oqc_metadata)


@pytest.fixture
def result_obj_1(task_metadata_shots, additional_metadata):
return GateModelTaskResult(
Expand All @@ -71,6 +146,20 @@ def result_obj_1(task_metadata_shots, additional_metadata):
)


@pytest.fixture
def result_rigetti(result_obj_1, additional_metadata_rigetti):
result = GateModelQuantumTaskResult.from_object(result_obj_1)
result.additional_metadata = additional_metadata_rigetti
return result


@pytest.fixture
def result_oqc(result_obj_1, additional_metadata_oqc):
result = GateModelQuantumTaskResult.from_object(result_obj_1)
result.additional_metadata = additional_metadata_oqc
return result


@pytest.fixture
def result_str_1(result_obj_1):
return result_obj_1.json()
Expand Down Expand Up @@ -234,6 +323,29 @@ def test_openqasm_shots_calculate_result_types(openqasm_result_obj_shots):
]


def test_get_compiled_circuit_rigetti(result_rigetti, quil_program):
"""Test get_compiled_circuit method."""
assert result_rigetti.get_compiled_circuit() == quil_program


def test_get_compiled_circuit_oqc(result_oqc, qasm2_program):
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: several of these tests/fixtures can be parametrized with pytest instead of duplicated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like I should be able to do something like this -- but afaiu parameter inputs can't be fixtures. Do you know how to do it, beyond accepting the fixture name as a string and looking up the function from it? If you have other more specific recommendations please let me know.

@pytest.mark.parametrize("result,program", [(result_rigetti, quil_program), (result_oqc, qasm2_program)])
def test_get_compiled_circuit(result, program):
    """Test get_compiled_circuit method."""
    assert result.get_compiled_circuit() == program

Copy link
Contributor Author

@laurencap laurencap Nov 8, 2023

Choose a reason for hiding this comment

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

Updated the result fixtures to reduce duplicated code

"""Test get_compiled_circuit method."""
assert result_oqc.get_compiled_circuit() == qasm2_program


def test_get_compiled_circuit_no_qhp_metadata(result_obj_1):
"""Test get_compiled_circuit method."""
result = GateModelQuantumTaskResult.from_object(result_obj_1)
assert result.get_compiled_circuit() is None


def test_get_compiled_circuit_no_metadata(result_obj_1):
"""Test that the method does not raise an error if metadata is missing."""
result = GateModelQuantumTaskResult.from_object(result_obj_1)
result.additional_metadata = None
assert result.get_compiled_circuit() is None


def test_measurement_counts_from_measurements():
measurements: np.ndarray = np.array(
[[1, 0, 1, 0], [0, 0, 0, 0], [1, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 1, 0]]
Expand Down