Skip to content

Commit

Permalink
only calculate op durations for ops in circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
flowerthrower committed May 16, 2024
1 parent 70b08ba commit c8cfba9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
47 changes: 22 additions & 25 deletions src/mqt/predictor/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,39 +86,36 @@ def expected_success_probability(qc: QuantumCircuit, device: Device, precision:
"""
res = 1.0

# collect gate durations for all qubits
# collect gate durations for operations in the circuit
op_times = []
for op in device.basis_gates:
if op == "barrier" or op == "id":
for instruction, qargs, _cargs in qc.data:
gate_type = instruction.name
if gate_type == "barrier" or gate_type == "id":
continue
if op in ["rxx", "rzz", "cx", "cz", "cp", "ecr", "xx_plus_yy"]:
for q0, q1 in device.coupling_map: # multi-qubit gates
try:
duration = device.get_two_qubit_gate_duration(op, q0, q1)
op_times.append((op, [q0, q1], duration, "s"))
except ValueError: # noqa:PERF203
pass # gate duration not available for this qubit pair
else: # single-qubit gates
for qubit in range(device.num_qubits):
try:
if op == "measure":
duration = device.get_readout_duration(qubit)
else:
duration = device.get_single_qubit_gate_duration(op, qubit)
op_times.append((op, [qubit], duration, "s"))
except ValueError: # noqa:PERF203
pass # gate duration not available for this qubit

# associate gate times for each qubit through asap scheduling
transpiled = transpile(qc, scheduling_method="asap", basis_gates=device.basis_gates, instruction_durations=op_times)
assert len(qargs) in [1, 2]
first_qubit_idx = calc_qubit_index(qargs, qc.qregs, 0)

if len(qargs) == 1: # single-qubit gate
if gate_type == "measure":
duration = device.get_readout_duration(first_qubit_idx)
else:
duration = device.get_single_qubit_gate_duration(gate_type, first_qubit_idx)
op_times.append((gate_type, [first_qubit_idx], duration, "s"))
else: # multi-qubit gate
second_qubit_idx = calc_qubit_index(qargs, qc.qregs, 1)
duration = device.get_two_qubit_gate_duration(gate_type, first_qubit_idx, second_qubit_idx)
op_times.append((gate_type, [first_qubit_idx, second_qubit_idx], duration, "s"))

# associate gate and idle (delay) times for each qubit through asap scheduling
scheduled_circ = transpile(
qc, scheduling_method="asap", basis_gates=device.basis_gates, instruction_durations=op_times
)
qubit_durations: dict[int, list[float]] = {qubit_index: [] for qubit_index in range(device.num_qubits)}

for instruction, qargs, _cargs in transpiled.data:
for instruction, qargs, _cargs in scheduled_circ.data:
gate_type = instruction.name
if gate_type == "barrier":
continue

assert len(qargs) in [1, 2]
first_qubit_idx = calc_qubit_index(qargs, qc.qregs, 0)

Expand Down
1 change: 1 addition & 0 deletions tests/compilation/test_predictor_rl.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_predictor_env_reset_from_string() -> None:
)
def test_qcompile_with_newly_trained_models(figure_of_merit: reward.figure_of_merit) -> None:
"""Test the qcompile function with a newly trained model.
Important: Those trained models are used in later tests and must not be deleted.
To test ESP as well, training must be done with a device that provides all relevant information (e.g. gate times).
"""
Expand Down
1 change: 1 addition & 0 deletions tests/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_create_result() -> None:
device = get_device_by_name("ibm_montreal")
assert device.num_qubits >= 10
qc = QuantumCircuit(10)
qc.measure_all()

res = create_tket_result(qc, device)
assert isinstance(res, Result)
Expand Down

0 comments on commit c8cfba9

Please sign in to comment.