1 from __future__
import annotations
3 from copy
import deepcopy
4 from functools
import lru_cache
7 from qiskit
import QuantumCircuit
8 from qiskit_aer
import AerSimulator
9 from qiskit_aer.noise
import (
10 amplitude_damping_error,
15 from benchmarks.density_matrix.partitioned_runtime.common
import (
16 PHASE3_RUNTIME_DENSITY_TOL,
17 PHASE3_RUNTIME_ENERGY_TOL,
18 build_density_comparison_metrics,
20 execute_partitioned_with_reference,
22 from benchmarks.density_matrix.planner_calibration.signals
import (
23 PLANNER_CALIBRATION_DENSITY_AWARE_OBJECTIVE_NAME,
24 apply_density_scores_and_rankings,
25 build_density_signal_record,
27 from benchmarks.density_matrix.planner_calibration.case_selection
import (
28 PLANNER_CALIBRATION_CASE_KIND_CONTINUITY,
29 PLANNER_CALIBRATION_CASE_KIND_MICROCASE,
30 iter_planner_calibration_candidate_cases,
32 from squander.partitioning.noisy_runtime
import PHASE3_RUNTIME_PATH_BASELINE
34 PLANNER_CALIBRATION_CALIBRATION_RECORD_SCHEMA_VERSION =
"phase3_planner_calibration_record_v1" 35 PLANNER_CALIBRATION_REFERENCE_BACKEND =
"qiskit_aer_density_matrix" 36 PLANNER_CALIBRATION_EXTERNAL_REFERENCE_CONTINUITY_QUBITS = (4,)
41 member
for partition
in descriptor_set.partitions
for member
in partition.members
46 op = descriptor_set.operations[member.canonical_operation_index]
47 return float(parameters[op.param_start])
51 qc: QuantumCircuit, descriptor_set, member, parameters: np.ndarray
53 op = descriptor_set.operations[member.canonical_operation_index]
56 theta, phi, lam = parameters[op.param_start : op.param_start + 3]
57 qiskit_theta = np.fmod(2.0 * float(theta), 4.0 * np.pi)
58 qiskit_phi = np.fmod(float(phi), 2.0 * np.pi)
59 qiskit_lambda = np.fmod(float(lam), 2.0 * np.pi)
60 qc.u(qiskit_theta, qiskit_phi, qiskit_lambda, op.target_qbit)
63 qc.cx(op.control_qbit, op.target_qbit)
66 "Unsupported Qiskit gate '{}' for planner calibration reference".format(op.name)
69 if op.kind ==
"noise":
72 if op.fixed_value
is not None 75 if op.name ==
"local_depolarizing":
76 qc.append(depolarizing_error(value, 1), [op.target_qbit])
78 if op.name ==
"amplitude_damping":
79 qc.append(amplitude_damping_error(value), [op.target_qbit])
81 if op.name ==
"phase_damping":
82 qc.append(phase_damping_error(value), [op.target_qbit])
85 "Unsupported Qiskit noise '{}' for planner calibration reference".format(op.name)
89 "Unsupported descriptor member kind '{}' for planner calibration reference".format(
97 parameters: np.ndarray,
99 qc = QuantumCircuit(descriptor_set.qbit_num)
102 qc.save_density_matrix()
103 simulator = AerSimulator(method=
"density_matrix")
104 result = simulator.run(qc, shots=1).
result()
105 return np.asarray(result.data()[
"density_matrix"])
109 return metadata[
"case_kind"] == PLANNER_CALIBRATION_CASE_KIND_MICROCASE
or (
110 metadata[
"case_kind"] == PLANNER_CALIBRATION_CASE_KIND_CONTINUITY
111 and metadata[
"qbit_num"]
in PLANNER_CALIBRATION_EXTERNAL_REFERENCE_CONTINUITY_QUBITS
118 parameters: np.ndarray,
123 descriptor_set, parameters, allow_fusion=
False 131 record[
"record_schema_version"] = PLANNER_CALIBRATION_CALIBRATION_RECORD_SCHEMA_VERSION
132 record[
"density_aware_objective_name"] = PLANNER_CALIBRATION_DENSITY_AWARE_OBJECTIVE_NAME
133 record[
"trace_deviation"] = runtime_result.trace_deviation
134 record[
"rho_is_valid"] = runtime_result.rho_is_valid
135 record[
"rho_is_valid_tol"] = 1e-10
136 record[
"runtime_path"] = PHASE3_RUNTIME_PATH_BASELINE
138 internal_density_pass = (
139 record[
"frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
140 and record[
"max_abs_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
141 and runtime_result.trace_deviation <= PHASE3_RUNTIME_DENSITY_TOL
142 and runtime_result.rho_is_valid
145 continuity_energy_error =
None 146 continuity_energy_pass =
True 147 if hamiltonian
is not None:
149 hamiltonian, runtime_result.density_matrix_numpy()
152 hamiltonian, np.asarray(reference_density.to_numpy())
154 continuity_energy_error = float(abs(runtime_energy_real - reference_energy_real))
155 continuity_energy_pass = continuity_energy_error <= PHASE3_RUNTIME_ENERGY_TOL
157 record[
"continuity_energy_error"] = continuity_energy_error
158 record[
"continuity_energy_pass"] = continuity_energy_pass
159 record[
"internal_correctness_pass"] = internal_density_pass
and continuity_energy_pass
162 record[
"external_reference_required"] = external_reference_required
163 record[
"reference_backend"] = (
164 PLANNER_CALIBRATION_REFERENCE_BACKEND
if external_reference_required
else None 166 if external_reference_required:
169 runtime_result.density_matrix, aer_density
171 external_energy_error =
None 172 if hamiltonian
is not None:
174 hamiltonian, runtime_result.density_matrix_numpy()
177 external_energy_error = float(abs(runtime_energy_real - aer_energy_real))
178 external_reference_pass = (
179 external_metrics[
"frobenius_norm_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
180 and external_metrics[
"max_abs_diff"] <= PHASE3_RUNTIME_DENSITY_TOL
182 external_energy_error
is None 183 or external_energy_error <= PHASE3_RUNTIME_ENERGY_TOL
186 record[
"external_frobenius_norm_diff"] = external_metrics[
"frobenius_norm_diff"]
187 record[
"external_max_abs_diff"] = external_metrics[
"max_abs_diff"]
188 record[
"external_energy_error"] = external_energy_error
189 record[
"external_reference_pass"] = external_reference_pass
191 record[
"external_frobenius_norm_diff"] =
None 192 record[
"external_max_abs_diff"] =
None 193 record[
"external_energy_error"] =
None 194 record[
"external_reference_pass"] =
True 196 record[
"counted_calibration_case"] = (
197 record[
"internal_correctness_pass"]
and record[
"external_reference_pass"]
202 @lru_cache(maxsize=1)
209 hamiltonian=hamiltonian,
def iter_planner_calibration_candidate_cases
def _build_planner_calibration_calibration_records_cached()
def apply_density_scores_and_rankings
def _member_parameter_value
def _apply_member_to_qiskit
def _requires_external_reference
def build_planner_calibration_calibration_records()
def build_planner_calibration_calibration_record
def execute_qiskit_density_reference
def _flatten_members(descriptor_set)
def build_density_comparison_metrics
def execute_partitioned_with_reference
def build_density_signal_record