Source code for qurry.qurrent.classical_shadow.utils

"""ShadowUnveil - Utils (:mod:`qurry.qurrent.classical_shadow.utils`)"""

from collections.abc import Hashable

from qiskit import QuantumCircuit, ClassicalRegister

from ...process.classical_shadow.unitary_set import U_M_GATES


[docs] def circuit_method_core( idx: int, target_circuit: QuantumCircuit, target_key: Hashable, exp_name: str, registers_mapping: dict[int, int], single_unitary_um: dict[int, int], ) -> QuantumCircuit: """Build the circuit for the experiment. Args: idx (int): Index of the quantum circuit. target_circuit (QuantumCircuit): Target circuit. target_key (Hashable): Target key. exp_name (str): Experiment name. registers_mapping (dict[int, int]): The mapping of the index of selected qubits to the index of the classical register. single_unitary_dict (dict[int, Operator]): The dictionary of the unitary operator. Returns: QuantumCircuit: The circuit for the experiment. """ old_name = "" if isinstance(target_circuit.name, str) else target_circuit.name qc_exp1 = target_circuit.copy( f"{exp_name}_{idx}" + "" if len(str(target_key)) < 1 else f".{target_key}" + "" if len(old_name) < 1 else f".{old_name}" ) c_meas1 = ClassicalRegister( len(registers_mapping), None if "m1" in [reg.name for reg in (qc_exp1.qregs + qc_exp1.cregs)] else "m1", ) qc_exp1.add_register(c_meas1) qc_exp1.barrier() qc_exp1.barrier() for qi, um in single_unitary_um.items(): qc_exp1.append(U_M_GATES[um], [qi]) for qi, ci in registers_mapping.items(): qc_exp1.measure(qc_exp1.qubits[qi], c_meas1[ci]) assert qc_exp1.cregs[-1] == c_meas1, ( f"The last classical register should be the measurement register {c_meas1}," + f" but get {qc_exp1.cregs[-1]} in {qc_exp1.cregs}. From {exp_name} on index {idx}." ) return qc_exp1