MitigationTask (qiskit_mitigation.mitigation_task)
MitigationTask
class MitigationTask
Bases: object
Calculates expectation values of observables using the executor.
This class is the base class for all mitigation methods. It enables preparing a QuantumProgram that can be executed on hardware using the Executor, and post process the results to calculate expectation values of given observables.
The task parameters should be given as input to the prepare function, and the relevant variables needed for post-processing are saved internally:
task = MitigationTask()
program = task.prepare(circuit=circuit,
observables=observables,
parameters=parameter_values)
job = executor.run(program)
results = job.result()
mitigated_result = task.postprocess(results)To calculate expectation values of a loaded job result, a task can be created from the job results with all the internal variables needed for post-processing. Alternatively, the variables required for post-processing can be given as input directly to the compute_expectation_value static method. Example for running post-processing for a loaded result:
task = MitigationTask()
program = task.prepare(circuit=circuit,
observables=observables,
parameters=parameter_values)
job_id = executor.run(program).job_id
job = service.job(job_id)
results = job.result()
task = load_tasks_from_result(results)[0]
mitigated_result = task.postprocess(results)Instantiate a MitigationTask.
Methods
find_unique_layers
find_unique_layers(circuit, custom_boxing_options=None, box_types='all', observables=None, trex=None)
Return the unique boxed layers of the given circuit using the given boxing options.
If the instance already holds a boxed circuit of the given circuit (for example, after calling prepare), and custom_boxing_options is None or equal to the saved boxing options, the saved boxed circuit is used instead of boxing the circuit again.
Parameters
-
circuit (QuantumCircuit) – The circuit to found its unique layers.
-
custom_boxing_options (dict | None) – The custom boxing options that will be used by
generate_boxing_pass_manager()function. -
box_types (Literal['gates', 'measurement', 'all']) – Can be either
"gates","measurement"or"all", corresponding to filter boxes with only gate layers, only measurement layers or all layers, respectively. -
observables (ObservablesArray |Sequence[SparsePauliOp] | None) –
If given, the layers are found for a circuit whose measurements are trimmed to the active and observed qubits, exactly as when
prepareis called withtrim_measurements=Trueand the same observables. IfNone(default), all the qubits are measured.WarningPass
observablesif and only iftrim_measurements=Trueis passed toprepare, together with the same observables. The measured qubits change the boxing, so layers learned with a different choice are not found byprepare. -
trex (bool | None) – Whether to add the boxing options that
prepareenforces when aTREXis used (measurement boxes annotated for all qubits). IfTrue, these options are always added. IfFalse, they are never added. IfNone(default), they are added if and only if this task has a TREX mitigation.
Returns
Unique boxed layers of the given circuit.
Raises
ValueError – If observables is given and no qubit is active or observed.
Return type
prepare
prepare(circuit, observables, parameters, custom_boxing_options=None, shots_per_randomization=64, num_randomizations=128, broadcast_obs_and_params=False, trex=None, quantum_program=None, trim_measurements=False)
Creates a QuantumProgram for executing via Executor.
Creates an item for a QuantumProgram, that can be executed via Executor. If a quantum_program is provided, the new item will be added to the existing program, otherwise, a new program will be created, containing only the created item. In the created program item the final measurement layer of the given circuit is removed and a new measurement layer with a dedicated classical register named _meas is added. The basic options creates an item without any mitigation methods applied to the circuit. If broadcast_obs_and_params is True, the observables and parameters will be broadcasted using the samplomatic broadcasting rules, to allow attaching some of the parameters to some of the observables. Otherwise, every parameter will be executed for each observable (outer product of the parameters and observables). Note that the post-processing of an outer product is usually faster.
Parameters
-
circuit (QuantumCircuit) – The quantum circuit.
-
observables (ObservablesArray |Sequence[SparsePauliOp]) – The observables to calculate their expectation values.
-
parameters (ndarray |BindingsArray | None) – The parameters of a parametric circuit.
-
custom_boxing_options (dict | None) – The custom boxing options that will be used by
generate_boxing_pass_manager()function. -
shots_per_randomization (int) – The number of shots per randomization.
-
num_randomizations (int) – The number of randomizations.
-
broadcast_obs_and_params (bool) – Whether to broadcast observables and parameters.
-
trex (TREX | None) – A TREX mitigation instance that will be used to mitigate readout errors.
-
quantum_program (QuantumProgram | None) – The quantum program to add an item for. If None, a new program will be created.
-
trim_measurements (bool) –
Whether to measure only the qubits that have instructions on them or are in the support of the observables, instead of all the qubits of the circuit. The measured qubits are available in
meas_qubitsafter the call. The results are expanded back to the full circuit width before post-processing.WarningTrimming changes the boxing of the circuit. To use noise learned for
PECorPEA, learn it on the layers found withobservablespassed tofind_combined_unique_layers()orfind_unique_layers(): passobservablesto the layer finder if and only iftrim_measurements=Trueis passed toprepare, and pass the same observables.
Returns
A QuantumProgram that can be executed via Executor.
Raises
ValueError – If trim_measurements is True and no qubit is active or observed.
Return type
postprocess
postprocess(results, measure_noise_data=None)
Process expectation values for a single item result.
Parameters
- results (QuantumProgramItemResult |QuantumProgramResult) – The execution results. Can be either the entire results object or the item result of this task. If TREX calibration task is added to the quantum program, its results will be used to compute the measure noise data if the entire results object is provided.
- measure_noise_data (PauliLindbladMap |ndarray | None) – The learned measurement noise data to use for TREX mitigation. If None and a calibration task is present the quantum program, the measure noise will be computed from the calibration task results if the entire results object is provided.
Returns
A PubResult which contains evs and std as fields in its data, where evs are expectation values, and std are the standard deviation of the expectation values. If broadcast_obs_and_params is set to True, the data will contain also a twirl_stds field which is the standard deviation between different randomizations.
Raises
- ValueError – If the task’s item result has no
'_meas'key. - ValueError – If the item result’s
'_meas'data has an invalid number of axes. - ValueError – If
param_shapeandobservables.shapecannot be broadcasted against each other, whilebroadcast_obs_and_paramswas set toTruein the task preparation.
Return type
compute_expectation_value
static compute_expectation_value(item_result, observables, param_shape=None, param_basis_pairs=None, meas_bases=None, broadcast_obs_and_params=False, measure_noise_data=None, meas_qubits=None, circuit_num_qubits=None)
Process expectation values for a single item result.
This function can be used to calculate expectation values for a single unmitigated item result without instantiating a new class instance.
Parameters
- item_result (QuantumProgramItemResult) – The item result.
- observables (ObservablesArray |Sequence[SparsePauliOp]) – The observables to calculate expectation values for.
- param_shape (tuple[int, ...] | None) – The shape of the parameter values.
- param_basis_pairs (list[tuple[tuple[int, ...], str]] | None) – The map between params ndindexes to measure basis.
- meas_bases (Sequence[Pauli] | Sequence[str] | PauliList | None) – A list of the measured Pauli bases. The
ith item is a measurement basis assumed to correspond to theith slice of the data initem_result. - broadcast_obs_and_params (bool) – Whether to broadcast observables and parameter values.
- measure_noise_data (PauliLindbladMap |ndarray | None) – Measurement noise calibration data for TREX mitigation.
- meas_qubits (list[int] | None) – Sorted list of the circuit qubit indices that were measured, if the task was prepared with
trim_measurements=True(available astask.meas_qubits). Must be given together withcircuit_num_qubits. IfNone, all the qubits are assumed to be measured, which gives wrong results for trimmed data. - circuit_num_qubits (int | None) – The total number of qubits in the circuit. Used together with
meas_qubitsto expand the data back to the full width. Must be given together withmeas_qubits.
Returns
A PubResult which contains evs and std as fields in its data, where evs are expectation values, and std are the standard deviation of the expectation values. If broadcast_obs_and_params is set to True, the data will contain also a twirl_stds field which is the standard deviation between different randomizations.
Raises
- ValueError – If
item_resulthas no'_meas'key. - ValueError – If
item_result['_meas']has invalid number of axes. - ValueError – If
param_shapeandobservables.shapecannot be broadcasted against each other. - ValueError – If only one of
meas_qubitsandcircuit_num_qubitsis given.
Return type
create_instance_from_passthrough_data
static create_instance_from_passthrough_data(passthrough, trex=None)
Create a MitigationTask instance from a passthrough dictionary loaded from a quantum program execution result.
Parameters
- passthrough (dict[str, Any]) – Passthrough_data dictionary loaded from a quantum program execution result.
- trex (TREX | None) – A TREX instance containing a calibration circuit results executed in the same quantum program. Should remain
Nonein case TREX mitigation was not used or a TREX calibration was not executed as part of thq same quantum program.
Returns
A MitigationTask instance.
Return type