"""QDK/Chemistry interoperability for Qiskit noise models."""
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from qiskit_aer.noise import NoiseModel, depolarizing_error
from qdk_chemistry.data.noise_models import QuantumErrorProfile, SupportedErrorTypes
from qdk_chemistry.utils import Logger
__all__ = ["get_noise_model_from_profile"]
[docs]
def get_noise_model_from_profile(
quantum_error_profile: QuantumErrorProfile, exclude_gates: list | None = None
) -> NoiseModel:
"""Convert profile to noise model.
Args:
quantum_error_profile: Quantum error profile
exclude_gates: Optional basis gates to use for noise model
Returns:
Configured noise model based on profile
"""
Logger.trace_entering()
noise_model = NoiseModel(basis_gates=quantum_error_profile.basis_gates)
for gate, error_rates in quantum_error_profile.errors.items():
if exclude_gates is not None and str(gate) in exclude_gates:
continue
for error_type, rate in error_rates.items():
if error_type == SupportedErrorTypes.DEPOLARIZING_ERROR:
num_qubits = _check_num_qubits(str(gate), noise_model)
noise_model.add_all_qubit_quantum_error(
depolarizing_error(rate, num_qubits),
[str(gate)], # Convert gate to string for Qiskit
)
elif error_type == SupportedErrorTypes.QUBIT_LOSS:
raise ValueError("Qubit loss error is not currently supported in Qiskit noise models.")
else:
raise ValueError(f"Unsupported error type: {error_type}")
return noise_model
def _check_num_qubits(gate: str, noise_model: NoiseModel) -> int:
"""Helper function to determine the number of qubits for a given gate."""
if gate in noise_model._1qubit_instructions: # noqa: SLF001
return 1
if gate in noise_model._2qubit_instructions: # noqa: SLF001
return 2
if gate in noise_model._3qubit_instructions: # noqa: SLF001
return 3
raise ValueError(f"Unsupported gate type: {gate}")