def _run( self, circuits: Sequence[QPROGRAM], force_run_all: bool = False, **kwargs: Any, ) -> Sequence[QuantumResult]: """Runs all input circuits using the least number of possible calls to the executor. Args: circuits: Sequence of circuits to execute using the executor. force_run_all: If True, force every circuit in the input sequence to be executed (if some are identical). Else, detects identical circuits and runs a minimal set. """ start_result_index = len(self._quantum_results) if force_run_all: to_run = circuits else: # Make circuits hashable. # Note: Assumes all circuits are the same type. # TODO: Bug! These conversions to/from Mitiq are not safe in that, # e.g., they do not preserve classical register structure in # Qiskit circuits, potentially causing executed results to be # incorrect. Safe conversions should follow the logic in # mitiq.interface.noise_scaling_converter. _, conversion_type = convert_to_mitiq(circuits[0]) hashable_circuits = [ convert_to_mitiq(circ)[0].freeze() for circ in circuits ] # Get the unique circuits and counts collection = Counter(hashable_circuits) to_run = [ convert_from_mitiq(circ.unfreeze(), conversion_type) for circ in collection.keys() ] if not self.can_batch: for circuit in to_run: self._call_executor(circuit, **kwargs) else: stop = len(to_run) step = self._max_batch_size for i in range(int(np.ceil(stop / step))): batch = to_run[i * step : (i + 1) * step] self._call_executor(batch, **kwargs) these_results = self._quantum_results[start_result_index:] if force_run_all: return these_results # Expand computed results to all results using counts. results_dict = dict(zip(collection.keys(), these_results)) results = [results_dict[key] for key in hashable_circuits] return results
def sample_circuit( ideal_circuit: QPROGRAM, representations: List[OperationRepresentation], random_state: Optional[Union[int, np.random.RandomState]] = None, ) -> Tuple[QPROGRAM, int, float]: """Samples an implementable circuit from the PEC representation of the input ideal circuit & returns this circuit as well as its sign and norm. This function iterates through each operation in the circuit and samples an implementable sequence. The returned sign (norm) is the product of signs (norms) sampled for each operation. Args: ideal_circuit: The ideal circuit from which an implementable circuit is sampled. representations: List of representations of every operation in the input circuit. If a representation cannot be found for an operation in the circuit, a ValueError is raised. random_state: Seed for sampling. Returns: imp_circuit: The sampled implementable circuit. sign: The sign associated to sampled_circuit. norm: The one norm of the PEC coefficients of the circuit. Raises: ValueError: If a representation is not found for an operation in the circuit. """ if isinstance(random_state, int): random_state = np.random.RandomState(random_state) # TODO: Likely to cause issues - conversions may introduce gates which are # not included in `decompositions`. ideal, rtype = convert_to_mitiq(ideal_circuit) # copy and remove all moments sampled_circuit = deepcopy(ideal)[0:0] # Iterate over all operations sign = 1 norm = 1.0 for op in ideal.all_operations(): # Ignore all measurements. if cirq.is_measurement(op): continue imp_seq, loc_sign, loc_norm = sample_sequence(cirq.Circuit(op), representations, random_state) cirq_seq, _ = convert_to_mitiq(imp_seq) sign *= loc_sign norm *= loc_norm sampled_circuit.append(cirq_seq.all_operations()) return convert_from_mitiq(sampled_circuit, rtype), sign, norm
def run( self, circuits: Sequence[QPROGRAM], force_run_all: bool = False, **kwargs, ) -> List[float]: """Runs all input circuits using the least number of possible calls to the executor. Args: circuits: Sequence of circuits to execute using the executor. force_run_all: If True, force every circuit in the input sequence to be executed (if some are identical). Else, detects identical circuits and runs a minimal set. """ if force_run_all: to_run = circuits else: # Make circuits hashable. # Note: Assumes all circuits are the same type. _, conversion_type = convert_to_mitiq(circuits[0]) hashable_circuits = [ convert_to_mitiq(circ)[0].freeze() for circ in circuits ] # Get the unique circuits and counts collection = Counter(hashable_circuits) to_run = [ convert_from_mitiq(circ.unfreeze(), conversion_type) for circ in collection.keys() ] if not self._can_batch: for circuit in to_run: self._call_executor(circuit, **kwargs) else: stop = len(to_run) step = self._max_batch_size for i in range(int(np.ceil(stop / step))): batch = to_run[i * step:(i + 1) * step] self._call_executor(batch, **kwargs) # Expand computed results to all results using counts. if force_run_all: return self._computed_results expval_dict = dict(zip(collection.keys(), self._computed_results)) results = [expval_dict[key] for key in hashable_circuits] return results
def __init__(self, circuit: QPROGRAM, channel_matrix: Optional[np.ndarray] = None) -> None: """Initializes a NoisyOperation. Args: circuit: A short circuit which, when executed on a given noisy quantum computer, generates a noisy channel. It typically contains a single-gate or a short sequence of gates. channel_matrix: Superoperator representation of the noisy channel which is generated when executing the input ``circuit`` on the noisy quantum computer. Raises: TypeError: If ``ideal`` is not a ``QPROGRAM``. """ self._native_circuit = circuit try: ideal_cirq, self._native_type = convert_to_mitiq(circuit) except (CircuitConversionError, UnsupportedCircuitError): raise TypeError(f"Arg `circuit` must be one of {QPROGRAM} but" f" was {type(circuit)}.") self._init_from_cirq(ideal_cirq, channel_matrix)
def test_execute_with_pec_mitigates_noise(circuit, executor, circuit_type): """Tests that execute_with_pec mitigates the error of a noisy expectation value. """ circuit = convert_from_mitiq(circuit, circuit_type) true_noiseless_value = 1.0 unmitigated = serial_executor(circuit) if circuit_type in ["qiskit", "pennylane"]: # Note this is an important subtlety necessary because of conversions. reps = get_pauli_and_cnot_representations( base_noise=BASE_NOISE, qubits=[cirq.NamedQubit(name) for name in ("q_0", "q_1")], ) # TODO: PEC with Qiskit is slow. # See https://github.com/unitaryfund/mitiq/issues/507. circuit, _ = convert_to_mitiq(circuit) else: reps = pauli_representations mitigated = execute_with_pec( circuit, executor, representations=reps, num_samples=100, force_run_all=False, random_state=101, ) error_unmitigated = abs(unmitigated - true_noiseless_value) error_mitigated = abs(mitigated - true_noiseless_value) assert error_mitigated < error_unmitigated assert np.isclose(mitigated, true_noiseless_value, atol=0.1)
def __init__(self, ideal: QPROGRAM, basis_expansion: Dict[NoisyOperation, float]) -> None: """Initializes an OperationRepresentation. Args: ideal: The ideal operation desired to be implemented. basis_expansion: Representation of the ideal operation in a basis of `NoisyOperation`s. Raises: TypeError: If all keys of `basis_expansion` are not instances of `NoisyOperation`s. """ self._native_ideal = ideal self._ideal, self._native_type = convert_to_mitiq(ideal) if not all( isinstance(op, NoisyOperation) for op in basis_expansion.keys()): raise TypeError("All keys of `basis_expansion` must be " "of type `NoisyOperation`.") self._basis_expansion = cirq.LinearDict(basis_expansion) self._norm = sum(abs(coeff) for coeff in self.coeffs) self._distribution = np.array(list(map(abs, self.coeffs))) / self.norm
def test_represent_operations_in_circuit_with_measurements( circuit_type: str, rep_function, ): """Tests measurements in circuit are ignored (not represented).""" q0, q1 = LineQubit.range(2) circ_mitiq = Circuit( X(q1), MeasurementGate(num_qubits=1)(q0), X(q1), MeasurementGate(num_qubits=1)(q0), ) circ = convert_from_mitiq(circ_mitiq, circuit_type) reps = rep_function(ideal_circuit=circ, noise_level=0.1) for op in convert_to_mitiq(circ)[0].all_operations(): found = False for rep in reps: if _equal(rep.ideal, Circuit(op), require_qubit_equality=True): found = True if isinstance(op.gate, MeasurementGate): assert not found else: assert found # Number of unique gates excluding measurement gates assert len(reps) == 1
def find_optimal_representation( ideal_operation: QPROGRAM, noisy_basis: NoisyBasis, tol: float = 1.0e-8, initial_guess: Optional[np.ndarray] = None, ) -> OperationRepresentation: r"""Returns the ``OperationRepresentaiton`` of the input ideal operation which minimizes the one-norm of the associated quasi-probability distribution. More precicely, it solve the following optimization problem: .. math:: \min_{{\eta_\alpha}} = \sum_\alpha |\eta_\alpha|, \text{ such that } \mathcal G = \sum_\alpha \eta_\alpha \mathcal O_\alpha, where :math:`\{\mathcal O_j\}` is the input basis of noisy operations. Args: ideal_operation: The ideal operation to represent. noisy_basis: The ``NoisyBasis`` in which the ``ideal_operation`` should be represented. It must contain ``NoisyOperation`` objects which are initialized with a numerical superoperator matrix. tol: The error tolerance for each matrix element of the represented operation. initial_guess: Optional initial guess for the coefficients :math:`\{ \eta_\alpha \}``. Returns: The optimal OperationRepresentation. """ ideal_cirq_circuit, _ = convert_to_mitiq(ideal_operation) ideal_matrix = kraus_to_super( cast(List[np.ndarray], kraus(ideal_cirq_circuit))) basis_set = noisy_basis.elements try: basis_matrices = [noisy_op.channel_matrix for noisy_op in basis_set] except ValueError as err: if str(err) == "The channel matrix is unknown.": raise ValueError( "The input noisy_basis should contain NoisyOperation objects" " which are initialized with a numerical superoperator matrix." ) else: raise err # pragma no cover # Run numerical optimization problem quasi_prob_dist = minimize_one_norm( ideal_matrix, basis_matrices, tol=tol, initial_guess=initial_guess, ) basis_expansion = {op: eta for op, eta in zip(basis_set, quasi_prob_dist)} return OperationRepresentation(ideal_operation, basis_expansion)
def serial_executor(circuit: QPROGRAM, noise: float = BASE_NOISE) -> float: """A noisy executor function which executes the input circuit with `noise` depolarizing noise and returns the expectation value of the ground state projector. Simulation will be slow for "large circuits" (> a few qubits). """ circuit, _ = convert_to_mitiq(circuit) return compute_density_matrix(circuit, noise_model=cirq.depolarize, noise_level=(noise, ))[0, 0].real
def serial_executor(circuit: QPROGRAM, noise: float = BASE_NOISE) -> float: """A noisy executor function which executes the input circuit with `noise` depolarizing noise and returns the expectation value of the ground state projector. Simulation will be slow for "large circuits" (> a few qubits). """ circuit, _ = convert_to_mitiq(circuit) # Ground state projector. d = 2**len(circuit.all_qubits()) obs = np.zeros(shape=(d, d), dtype=np.float32) obs[0, 0] = 1.0 return noisy_simulation(circuit, noise, obs)
def sample_sequence( ideal_operation: QPROGRAM, representations: List[OperationRepresentation], random_state: Optional[Union[int, np.random.RandomState]] = None, ) -> Tuple[QPROGRAM, int, float]: """Samples an implementable sequence from the PEC representation of the input ideal operation & returns this sequence as well as its sign and norm. For example, if the ideal operation is U with representation U = a A + b B, then this function returns A with probability :math:`|a| / (|a| + |b|)` and B with probability :math:`|b| / (|a| + |b|)`. Also returns sign(a) (sign(b)) and :math:`|a| + |b|` if A (B) is sampled. Note that the ideal operation can be a sequence of operations (circuit), for instance U = V W, as long as a representation is known. Similarly, A and B can be sequences of operations (circuits) or just single operations. Args: ideal_operation: The ideal operation from which an implementable sequence is sampled. representations: A list of representations of ideal operations in a noisy basis. If no representation is found for `ideal_operation`, a ValueError is raised. random_state: Seed for sampling. Returns: imp_seq: The sampled implementable sequence as QPROGRAM. sign: The sign associated to sampled sequence. norm: The one-norm of the decomposition coefficients. Raises: ValueError: If no representation is found for `ideal_operation`. """ # Grab the representation for the given ideal operation. ideal, _ = convert_to_mitiq(ideal_operation) operation_representation = None for representation in representations: if _equal(representation.ideal, ideal, require_qubit_equality=True): operation_representation = representation break if operation_representation is None: raise ValueError( f"Representation of ideal operation \n\n{ideal_operation}\n\n not " "found in provided representations.") # Sample from this representation. noisy_operation, sign, _ = operation_representation.sample(random_state) return noisy_operation.ideal_circuit(), sign, operation_representation.norm
def represent_operations_in_circuit_with_local_depolarizing_noise( ideal_circuit: QPROGRAM, noise_level: float) -> List[OperationRepresentation]: """Iterates over all unique operations of the input ``ideal_circuit`` and, for each of them, generates the corresponding quasi-probability representation (linear combination of implementable noisy operations). This function assumes that the tensor product of ``k`` single-qubit depolarizing channels affects each implemented operation, where ``k`` is the number of qubits associated to the operation. This function internally calls :func:`represent_operation_with_local_depolarizing_noise` (more details about the quasi-probability representation can be found in its docstring). Args: ideal_circuit: The ideal circuit, whose ideal operations should be represented. noise_level: The (gate-independent) depolarizing noise level. Returns: The list of quasi-probability representations associated to the operations of the input ``ideal_circuit``. .. note:: Measurement gates are ignored (not represented). .. note:: The returned representations are always defined in terms of Cirq circuits, even if the input is not a ``cirq.Circuit``. """ circ, _ = convert_to_mitiq(ideal_circuit) representations = [] for op in set(circ.all_operations()): if is_measurement(op): continue representations.append( represent_operation_with_local_depolarizing_noise( Circuit(op), noise_level, )) return representations
def test_represent_operations_in_circuit_local(circuit_type: str): """Tests all operation representations are created.""" qreg = LineQubit.range(2) circ_mitiq = Circuit([CNOT(*qreg), H(qreg[0]), Y(qreg[1]), CNOT(*qreg)]) circ = convert_from_mitiq(circ_mitiq, circuit_type) reps = represent_operations_in_circuit_with_local_depolarizing_noise( ideal_circuit=circ, noise_level=0.1, ) for op in convert_to_mitiq(circ)[0].all_operations(): found = False for rep in reps: if _equal(rep.ideal, Circuit(op), require_qubit_equality=True): found = True assert found # The number of reps. should match the number of unique operations assert len(reps) == 3
def __init__(self, ideal: QPROGRAM, real: Optional[np.ndarray] = None) -> None: """Initializes a NoisyOperation. Args: ideal: The operation a noiseless quantum computer would implement. real: Superoperator representation of the actual operation implemented on a noisy quantum computer, if known. Raises: TypeError: If ideal is not a QPROGRAM. """ self._native_ideal = ideal try: ideal_cirq, self._native_type = convert_to_mitiq(ideal) except (CircuitConversionError, UnsupportedCircuitError): raise TypeError( f"Arg `ideal` must be one of {QPROGRAM} but was {type(ideal)}." ) self._init_from_cirq(ideal_cirq, real)
def execute_with_pec( circuit: QPROGRAM, executor: Callable, representations: List[OperationRepresentation], precision: float = 0.03, num_samples: Optional[int] = None, force_run_all: bool = True, random_state: Optional[Union[int, np.random.RandomState]] = None, full_output: bool = False, ) -> Union[float, Tuple[float, Dict[str, Any]]]: r"""Evaluates the expectation value associated to the input circuit using probabilistic error cancellation (PEC) [Temme2017]_ [Endo2018]_. This function implements PEC by: 1. Sampling different implementable circuits from the quasi-probability representation of the input circuit; 2. Evaluating the noisy expectation values associated to the sampled circuits (through the "executor" function provided by the user); 3. Estimating the ideal expectation value from a suitable linear combination of the noisy ones. Args: circuit: The input circuit to execute with error-mitigation. executor: A function which executes a circuit (sequence of circuits) and returns an expectation value (sequence of expectation values). representations: Representations (basis expansions) of each operation in the input circuit. precision: The desired estimation precision (assuming the observable is bounded by 1). The number of samples is deduced according to the formula (one_norm / precision) ** 2, where 'one_norm' is related to the negativity of the quasi-probability representation [Temme2017]_. If 'num_samples' is explicitly set by the user, 'precision' is ignored and has no effect. num_samples: The number of noisy circuits to be sampled for PEC. If not given, this is deduced from the argument 'precision'. force_run_all: If True, all sampled circuits are executed regardless of uniqueness, else a minimal unique set is executed. random_state: Seed for sampling circuits. full_output: If False only the average PEC value is returned. If True a dictionary containing all PEC data is returned too. Returns: pec_value: The PEC estimate of the ideal expectation value associated to the input circuit. pec_data: A dictionary which contains all the raw data involved in the PEC process (including the PEC estimation error). The error is estimated as pec_std / sqrt(num_samples), where 'pec_std' is the standard deviation of the PEC samples, i.e., the square root of the mean squared deviation of the sampled values from 'pec_value'. This is returned only if ``full_output`` is ``True``. .. [Endo2018] : Suguru Endo, Simon C. Benjamin, Ying Li, "Practical Quantum Error Mitigation for Near-Future Applications" *Phys. Rev. **X 8**, 031027 (2018), (https://arxiv.org/abs/1712.09271). .. [Takagi2020] : Ryuji Takagi, "Optimal resource cost for error mitigation," (https://arxiv.org/abs/2006.12509). """ if isinstance(random_state, int): random_state = np.random.RandomState(random_state) if not (0 < precision <= 1): raise ValueError( "The value of 'precision' should be within the interval (0, 1]," f" but precision is {precision}.") # Get the 1-norm of the circuit quasi-probability representation _, _, norm = sample_circuit(circuit, representations) # Deduce the number of samples (if not given by the user) if not isinstance(num_samples, int): num_samples = int((norm / precision)**2) # Issue warning for very large sample size if num_samples > 10**5: warnings.warn(_LARGE_SAMPLE_WARN, LargeSampleWarning) sampled_circuits = [] signs = [] converted_circuit, _ = convert_to_mitiq(circuit) for _ in range(num_samples): sampled_circuit, sign, _ = sample_circuit(converted_circuit, representations, random_state) sampled_circuits.append(sampled_circuit) signs.append(sign) # Execute all sampled circuits collected_executor = generate_collected_executor( executor, force_run_all=force_run_all) exp_values = collected_executor(sampled_circuits) # Evaluate unbiased estimators [Temme2017] [Endo2018] [Takagi2020] unbiased_estimators = [norm * s * val for s, val in zip(signs, exp_values)] pec_value = np.average(unbiased_estimators) if not full_output: return pec_value # Build dictionary with additional results and data pec_data: Dict[str, Any] = {} pec_data = { "num_samples": num_samples, "precision": precision, "pec_value": pec_value, "pec_error": np.std(unbiased_estimators) / np.sqrt(num_samples), "unbiased_estimators": unbiased_estimators, "measured_expectation_values": exp_values, "sampled_circuits": sampled_circuits, } return pec_value, pec_data
def execute(circuit: QPROGRAM) -> np.ndarray: return compute_density_matrix(convert_to_mitiq(circuit)[0])
def sample_circuit( ideal_circuit: QPROGRAM, representations: Sequence[OperationRepresentation], random_state: Optional[Union[int, np.random.RandomState]] = None, num_samples: int = 1, ) -> Tuple[List[QPROGRAM], List[int], float]: """Samples a list of implementable circuits from the quasi-probability representation of the input ideal circuit. Returns the list of circuits, the corresponding list of signs and the one-norm of the quasi-probability representation (of the full circuit). Args: ideal_circuit: The ideal circuit from which an implementable circuit is sampled. representations: List of representations of every operation in the input circuit. If a representation cannot be found for an operation in the circuit, a ValueError is raised. random_state: Seed for sampling. num_samples: The number of samples. Returns: The tuple (``sampled_circuits``, ``signs``, ``norm``) where ``sampled_circuits`` are the sampled implementable circuits, ``signs`` are the signs associated to sampled_circuits and ``norm`` is the one-norm of the circuit representation. Raises: ValueError: If a representation is not found for an operation in the circuit. """ if isinstance(random_state, int): random_state = np.random.RandomState(random_state) # TODO: Likely to cause issues - conversions may introduce gates which are # not included in `decompositions`. ideal, rtype = convert_to_mitiq(ideal_circuit) # copy and remove all moments sampled_circuits = [deepcopy(ideal)[0:0] for _ in range(num_samples)] sampled_signs = [1 for _ in range(num_samples)] norm = 1.0 for op in ideal.all_operations(): # Ignore all measurements. if cirq.is_measurement(op): continue sequences, loc_signs, loc_norm = sample_sequence( cirq.Circuit(op), representations, num_samples=num_samples, random_state=random_state, ) norm *= loc_norm for j in range(num_samples): sampled_signs[j] *= loc_signs[j] cirq_seq, _ = convert_to_mitiq(sequences[j]) sampled_circuits[j].append(cirq_seq.all_operations()) native_circuits = [convert_from_mitiq(c, rtype) for c in sampled_circuits] return native_circuits, sampled_signs, norm
def execute_with_pec( circuit: QPROGRAM, executor: Union[Executor, Callable[[QPROGRAM], QuantumResult]], observable: Optional[Observable] = None, *, representations: Sequence[OperationRepresentation], precision: float = 0.03, num_samples: Optional[int] = None, force_run_all: bool = True, random_state: Optional[Union[int, np.random.RandomState]] = None, full_output: bool = False, ) -> Union[float, Tuple[float, Dict[str, Any]]]: r"""Estimates the error-mitigated expectation value associated to the input circuit, via the application of probabilistic error cancellation (PEC). [Temme2017]_ [Endo2018]_. This function implements PEC by: 1. Sampling different implementable circuits from the quasi-probability representation of the input circuit; 2. Evaluating the noisy expectation values associated to the sampled circuits (through the "executor" function provided by the user); 3. Estimating the ideal expectation value from a suitable linear combination of the noisy ones. Args: circuit: The input circuit to execute with error-mitigation. executor: A Mitiq executor that executes a circuit and returns the unmitigated ``QuantumResult`` (e.g. an expectation value). observable: Observable to compute the expectation value of. If None, the `executor` must return an expectation value. Otherwise, the `QuantumResult` returned by `executor` is used to compute the expectation of the observable. representations: Representations (basis expansions) of each operation in the input circuit. precision: The desired estimation precision (assuming the observable is bounded by 1). The number of samples is deduced according to the formula (one_norm / precision) ** 2, where 'one_norm' is related to the negativity of the quasi-probability representation [Temme2017]_. If 'num_samples' is explicitly set by the user, 'precision' is ignored and has no effect. num_samples: The number of noisy circuits to be sampled for PEC. If not given, this is deduced from the argument 'precision'. force_run_all: If True, all sampled circuits are executed regardless of uniqueness, else a minimal unique set is executed. random_state: Seed for sampling circuits. full_output: If False only the average PEC value is returned. If True a dictionary containing all PEC data is returned too. Returns: The tuple ``(pec_value, pec_data)`` where ``pec_value`` is the expectation value estimated with PEC and ``pec_data`` is a dictionary which contains all the raw data involved in the PEC process (including the PEC estimation error). The error is estimated as ``pec_std / sqrt(num_samples)``, where ``pec_std`` is the standard deviation of the PEC samples, i.e., the square root of the mean squared deviation of the sampled values from ``pec_value``. If ``full_output`` is ``True``, only ``pec_value`` is returned. .. [Endo2018] : Suguru Endo, Simon C. Benjamin, Ying Li, "Practical Quantum Error Mitigation for Near-Future Applications" *Phys. Rev. **X 8**, 031027 (2018), (https://arxiv.org/abs/1712.09271). .. [Takagi2020] : Ryuji Takagi, "Optimal resource cost for error mitigation," (https://arxiv.org/abs/2006.12509). """ if isinstance(random_state, int): random_state = np.random.RandomState(random_state) if not (0 < precision <= 1): raise ValueError( "The value of 'precision' should be within the interval (0, 1]," f" but precision is {precision}.") converted_circuit, input_type = convert_to_mitiq(circuit) # Get the 1-norm of the circuit quasi-probability representation _, _, norm = sample_circuit( converted_circuit, representations, num_samples=1, ) # Deduce the number of samples (if not given by the user) if not isinstance(num_samples, int): num_samples = int((norm / precision)**2) # Issue warning for very large sample size if num_samples > 10**5: warnings.warn(_LARGE_SAMPLE_WARN, LargeSampleWarning) # Sample all the circuits sampled_circuits, signs, _ = sample_circuit( converted_circuit, representations, random_state=random_state, num_samples=num_samples, ) # Convert back to the original type sampled_circuits = [ convert_from_mitiq(cast(CirqCircuit, c), input_type) for c in sampled_circuits ] # Execute all sampled circuits if not isinstance(executor, Executor): executor = Executor(executor) results = executor.evaluate(sampled_circuits, observable, force_run_all) # Evaluate unbiased estimators [Temme2017] [Endo2018] [Takagi2020] unbiased_estimators = [ norm * s * val # type: ignore[operator] for s, val in zip(signs, results) ] pec_value = np.average(unbiased_estimators) if not full_output: return pec_value # Build dictionary with additional results and data pec_data: Dict[str, Any] = { "num_samples": num_samples, "precision": precision, "pec_value": pec_value, "pec_error": np.std(unbiased_estimators) / np.sqrt(num_samples), "unbiased_estimators": unbiased_estimators, "measured_expectation_values": results, "sampled_circuits": sampled_circuits, } return pec_value, pec_data
def simulate(circuit: QPROGRAM) -> np.ndarray: return compute_density_matrix(convert_to_mitiq(circuit)[0], noise_level=(0, ))
def amp_damp_executor(circuit: QPROGRAM, noise: float = 0.005) -> float: circuit, _ = convert_to_mitiq(circuit) return compute_density_matrix(circuit, noise_model=cirq.amplitude_damp, noise_level=(noise, ))[0, 0].real
def represent_operation_with_global_depolarizing_noise( ideal_operation: QPROGRAM, noise_level: float) -> OperationRepresentation: r"""As described in [Temme2017]_, this function maps an ``ideal_operation`` :math:`\mathcal{U}` into its quasi-probability representation, which is a linear combination of noisy implementable operations :math:`\sum_\alpha \eta_{\alpha} \mathcal{O}_{\alpha}`. This function assumes a depolarizing noise model and, more precicely, that the following noisy operations are implementable :math:`\mathcal{O}_{\alpha} = \mathcal{D} \circ \mathcal P_\alpha \circ \mathcal{U}`, where :math:`\mathcal{U}` is the unitary associated to the input ``ideal_operation`` acting on :math:`k` qubits, :math:`\mathcal{P}_\alpha` is a Pauli operation and :math:`\mathcal{D}(\rho) = (1 - \epsilon) \rho + \epsilon I/2^k` is a depolarizing channel (:math:`\epsilon` is a simple function of ``noise_level``). For a single-qubit ``ideal_operation``, the representation is as follows: .. math:: \mathcal{U}_{\beta} = \eta_1 \mathcal{O}_1 + \eta_2 \mathcal{O}_2 + \eta_3 \mathcal{O}_3 + \eta_4 \mathcal{O}_4 .. math:: \eta_1 =1 + \frac{3}{4} \frac{\epsilon}{1- \epsilon}, \qquad \mathcal{O}_1 = \mathcal{D} \circ \mathcal{I} \circ \mathcal{U} \eta_2 =- \frac{1}{4}\frac{\epsilon}{1- \epsilon} , \qquad \mathcal{O}_2 = \mathcal{D} \circ \mathcal{X} \circ \mathcal{U} \eta_3 =- \frac{1}{4}\frac{\epsilon}{1- \epsilon} , \qquad \mathcal{O}_3 = \mathcal{D} \circ \mathcal{Y} \circ \mathcal{U} \eta_4 =- \frac{1}{4}\frac{\epsilon}{1- \epsilon} , \qquad \mathcal{O}_4 = \mathcal{D} \circ \mathcal{Z} \circ \mathcal{U} It was proven in [Takagi2020]_ that, under suitable assumptions, this representation is optimal (minimum 1-norm). Args: ideal_operation: The ideal operation (as a QPROGRAM) to represent. noise_level: The noise level (as a float) of the depolarizing channel. Returns: The quasi-probability representation of the ``ideal_operation``. .. note:: This representation is based on the ideal assumption that one can append Pauli gates to a noisy operation without introducing additional noise. For a beckend which violates this assumption, it remains a good approximation for small values of ``noise_level``. .. note:: The input ``ideal_operation`` is typically a QPROGRAM with a single gate but could also correspond to a sequence of more gates. This is possible as long as the unitary associated to the input QPROGRAM, followed by a single final depolarizing channel, is physically implementable. """ circ, in_type = convert_to_mitiq(ideal_operation) post_ops: List[List[Operation]] qubits = circ.all_qubits() # The single-qubit case: linear combination of 1Q Paulis if len(qubits) == 1: q = tuple(qubits)[0] epsilon = 4 / 3 * noise_level alpha_pos = 1 + ((3 / 4) * epsilon / (1 - epsilon)) alpha_neg = -(1 / 4) * epsilon / (1 - epsilon) alphas = [alpha_pos] + 3 * [alpha_neg] post_ops = [[]] # for alpha_pos, we do nothing, rather than I post_ops += [[P(q)] for P in [X, Y, Z]] # 1Q Paulis # The two-qubit case: linear combination of 2Q Paulis elif len(qubits) == 2: q0, q1 = qubits epsilon = 16 / 15 * noise_level alpha_pos = 1 + ((15 / 16) * epsilon / (1 - epsilon)) alpha_neg = -(1 / 16) * epsilon / (1 - epsilon) alphas = [alpha_pos] + 15 * [alpha_neg] post_ops = [[]] # for alpha_pos, we do nothing, rather than I x I post_ops += [[P(q0)] for P in [X, Y, Z]] # 1Q Paulis for q0 post_ops += [[P(q1)] for P in [X, Y, Z]] # 1Q Paulis for q1 post_ops += [[Pi(q0), Pj(q1)] for Pi in [X, Y, Z] for Pj in [X, Y, Z]] # 2Q Paulis else: raise ValueError("Can only represent single- and two-qubit gates." "Consider pre-compiling your circuit.") # Basis of implementable operations as circuits imp_op_circuits = [circ + Circuit(op) for op in post_ops] # Convert back to input type imp_op_circuits = [convert_from_mitiq(c, in_type) for c in imp_op_circuits] # Build basis_expantion expansion = {NoisyOperation(c): a for c, a in zip(imp_op_circuits, alphas)} return OperationRepresentation(ideal_operation, expansion)
def represent_operation_with_local_depolarizing_noise( ideal_operation: QPROGRAM, noise_level: float) -> OperationRepresentation: r"""As described in [Temme2017]_, this function maps an ``ideal_operation`` :math:`\mathcal{U}` into its quasi-probability representation, which is a linear combination of noisy implementable operations :math:`\sum_\alpha \eta_{\alpha} \mathcal{O}_{\alpha}`. This function assumes a (local) single-qubit depolarizing noise model even for multi-qubit operations. More precicely, it assumes that the following noisy operations are implementable :math:`\mathcal{O}_{\alpha} = \mathcal{D}^{\otimes k} \circ \mathcal P_\alpha \circ \mathcal{U}`, where :math:`\mathcal{U}` is the unitary associated to the input ``ideal_operation`` acting on :math:`k` qubits, :math:`\mathcal{P}_\alpha` is a Pauli operation and :math:`\mathcal{D}(\rho) = (1 - \epsilon) \rho + \epsilon I/2` is a single-qubit depolarizing channel (:math:`\epsilon` is a simple function of ``noise_level``). More information about the quasi-probability representation for a depolarizing noise channel can be found in: :func:`represent_operation_with_global_depolarizing_noise`. Args: ideal_operation: The ideal operation (as a QPROGRAM) to represent. noise_level: The noise level of each depolarizing channel. Returns: The quasi-probability representation of the ``ideal_operation``. .. note:: The input ``ideal_operation`` is typically a QPROGRAM with a single gate but could also correspond to a sequence of more gates. This is possible as long as the unitary associated to the input QPROGRAM, followed by a single final depolarizing channel, is physically implementable. .. [Temme2017] : Kristan Temme, Sergey Bravyi, Jay M. Gambetta, "Error mitigation for short-depth quantum circuits," *Phys. Rev. Lett.* **119**, 180509 (2017), (https://arxiv.org/abs/1612.02058). """ circ, in_type = convert_to_mitiq(ideal_operation) qubits = circ.all_qubits() if len(qubits) == 1: return represent_operation_with_global_depolarizing_noise( ideal_operation, noise_level, ) # The two-qubit case: tensor product of two depolarizing channels. elif len(qubits) == 2: q0, q1 = qubits # Single-qubit representation coefficients. epsilon = noise_level * 4 / 3 c_neg = -(1 / 4) * epsilon / (1 - epsilon) c_pos = 1 - 3 * c_neg imp_op_circuits = [] alphas = [] # The zero-pauli term in the linear combination imp_op_circuits.append(circ) alphas.append(c_pos * c_pos) # The single-pauli terms in the linear combination for qubit in qubits: for pauli in [X, Y, Z]: imp_op_circuits.append(circ + Circuit(pauli(qubit))) alphas.append(c_neg * c_pos) # The two-pauli terms in the linear combination for pauli_0, pauli_1 in product([X, Y, Z], repeat=2): imp_op_circuits.append(circ + Circuit(pauli_0(q0), pauli_1(q1))) alphas.append(c_neg * c_neg) else: raise ValueError("Can only represent single- and two-qubit gates." "Consider pre-compiling your circuit.") # Convert back to input type imp_op_circuits = [convert_from_mitiq(c, in_type) for c in imp_op_circuits] # Build basis_expantion expansion = {NoisyOperation(c): a for c, a in zip(imp_op_circuits, alphas)} return OperationRepresentation(ideal_operation, expansion)
def test_from_mitiq(to_type): converted_circuit = convert_from_mitiq(cirq_circuit, to_type) circuit, input_type = convert_to_mitiq(converted_circuit) assert _equal(circuit, cirq_circuit) assert input_type == to_type
def test_to_mitiq(circuit): converted_circuit, input_type = convert_to_mitiq(circuit) assert _equal(converted_circuit, cirq_circuit) assert input_type in circuit.__module__
def test_to_mitiq_bad_types(item): with pytest.raises( UnsupportedCircuitError, match="Could not determine the package of the input circuit.", ): convert_to_mitiq(item)
def sample_sequence( ideal_operation: QPROGRAM, representations: Sequence[OperationRepresentation], random_state: Optional[Union[int, np.random.RandomState]] = None, num_samples: int = 1, ) -> Tuple[List[QPROGRAM], List[int], float]: """Samples a list of implementable sequences from the quasi-probability representation of the input ideal operation. Returns the list of sequences, the corresponding list of signs and the one-norm of the quasi-probability representation (of the input operation). For example, if the ideal operation is U with representation U = a A + b B, then this function returns A with probability :math:`|a| / (|a| + |b|)` and B with probability :math:`|b| / (|a| + |b|)`. Also returns sign(a) (sign(b)) and :math:`|a| + |b|` if A (B) is sampled. Note that the ideal operation can be a sequence of operations (circuit), for instance U = V W, as long as a representation is known. Similarly, A and B can be sequences of operations (circuits) or just single operations. Args: ideal_operation: The ideal operation from which an implementable sequence is sampled. representations: A list of representations of ideal operations in a noisy basis. If no representation is found for `ideal_operation`, a ValueError is raised. random_state: Seed for sampling. num_samples: The number of samples. Returns: The tuple (``sequences``, ``signs``, ``norm``) where ``sequences`` are the sampled sequences, ``signs`` are the signs associated to the sampled ``sequences`` and ``norm`` is the one-norm of the quasi-probability distribution. Raises: ValueError: If no representation is found for `ideal_operation`. """ # Grab the representation for the given ideal operation. ideal, _ = convert_to_mitiq(ideal_operation) operation_representation = None for representation in representations: if _equal(representation._ideal, ideal, require_qubit_equality=True): operation_representation = representation break if operation_representation is None: warnings.warn( UserWarning(f"No representation found for \n\n{ideal_operation}.")) return ( [ideal_operation] * num_samples, [1] * num_samples, 1.0, ) # Sample from this representation. norm = operation_representation.norm sequences = [] signs = [] for _ in range(num_samples): noisy_op, sign, _ = operation_representation.sample(random_state) sequences.append(noisy_op.circuit()) signs.append(sign) return sequences, signs, norm