qermit.noise_model

class qermit.noise_model.noise_model.ErrorDistribution(distribution, rng=Generator(PCG64) at 0x7FDA00369FC0)[source]

Model of a Pauli error channel. Contains utilities to analyse and sample from distributions of errors.

Attributes:

distribution: Dictionary mapping a string of Pauli errors to the probability that they occur. rng: Randomness generator.

Parameters:
  • distribution (Dict[Tuple[Pauli, ...], float]) – Dictionary mapping a string of Pauli errors to the probability that they occur.

  • rng (Generator) – Randomness generator, defaults to np.random.default_rng()

Raises:

Exception – Raised if error probabilities sum to greater than 1.

classmethod from_dict(distribution_dict)[source]

Generates ErrorDistribution from json serialisable representation.

Parameters:

distribution_dict (List[Dict[str, Union[List[int], float]]]) – List of dictionaries, each of which map a property of the distribution to its value.

Return type:

ErrorDistribution

Returns:

ErrorDistribution created from serialised representation.

classmethod from_ptm(ptm, pauli_index)[source]

Convert a Pauli Transfer Matrix (PTM) to an error distribution.

Parameters:
  • ptm (ndarray[Any, dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]]) – Pauli Transfer Matrix to convert. Should be a 4^n by 4^n matrix where n is the number of qubits.

  • pauli_index (Dict[Tuple[Pauli, ...], int]) – A dictionary mapping Pauli errors to their index in the PTM.

Return type:

ErrorDistribution

Returns:

The converted error distribution.

property identity_error_rate: float

The rate at which no error occurs.

Returns:

Rate at which no error occurs. Calculated as 1 minus the total error rate of error in this distribution.

classmethod mixture(distribution_list)[source]

Generates the distribution corresponding to the mixture of a list of distributions.

Parameters:

distribution_list (List[ErrorDistribution]) – List of instances of ErrorDistribution.

Return type:

ErrorDistribution

Returns:

Mixture distribution.

property n_qubits: int

The number of qubits this error distribution acts on.

order(reverse=True)[source]

Reorders the distribution dictionary based on probabilities.

Parameters:

reverse (bool) – Order from high to low, defaults to True

plot()[source]

Generates plot of distribution.

reset_rng(rng)[source]

Reset randomness generator.

Parameters:

rng (Generator) – Randomness generator.

sample()[source]

Draw sample from distribution.

Return type:

Optional[Tuple[Pauli, ...]]

Returns:

Either one of the pauli strings in the support of the distribution, or None. None can be returned if the total proability of the distribution not 1, and should be interpreted as the the unspecified support.

scale(scaling_factor)[source]

Scale the error rates of this error distribution. This is done by converting the error distribution to a PTM, scaling that matrix appropriately, and converting back to a new error distribution.

Parameters:

scaling_factor (float) – The factor by which the noise should be scaled.

Return type:

ErrorDistribution

Returns:

A new error distribution with the noise scaled.

to_dict()[source]

Produces json serialisable representation of ErrorDistribution.

Return type:

List[Dict[str, Union[List[int], float]]]

Returns:

Json serialisable representation of ErrorDistribution.

to_ptm()[source]

Convert error distribution to Pauli Transfer Matrix (PTM) form.

Return type:

Tuple[ndarray[Any, dtype[TypeVar(_ScalarType_co, bound= generic, covariant=True)]], Dict[Tuple[Pauli, ...], int]]

Returns:

PTM of error distribution and Pauli index dictionary. The Pauli index dictionary maps Pauli errors to their index in the PTM

class qermit.noise_model.noise_model.NoiseModel(noise_model)[source]

Module for managing and executing a circuit noise model. In particular error models are assigned to each gate, and logical errors from circuits can be sampled.

Attributes:
noise_model: Mapping from gates to the error model which corresponds

to that gate.

Parameters:

noise_model (Dict[OpType, ErrorDistribution]) – Map from gates to their error models.

counter_propagate(cliff_circ, n_counts, **kwargs)[source]

Generate random noisy instances of the given circuit and propagate the noise to create a counter of logical errors. Note that kwargs are passed onto random_propagate.

Parameters:
  • cliff_circ (Circuit) – Circuit to be simulated. This should be a Clifford circuit.

  • n_counts (int) – Number of random instances.

Return type:

Counter[QermitPauli]

Returns:

Counter of logical errors.

classmethod from_dict(noise_model_dict)[source]

Convert JSON serialised version of noise model back to an instance of NoiseModel.

Parameters:

noise_model_dict (Dict[str, List[Dict[str, Union[List[int], float]]]]) – JSON serialised version of NoiseModel

Return type:

NoiseModel

Returns:

Instance of noise model corresponding to JSON serialised version.

get_effective_pre_error_distribution(cliff_circ, n_rand=1000, **kwargs)[source]

Retrieve the effective noise model of a given circuit. This is to say, repeatedly generate circuits with coherent noise added at random. Push all errors to the front of the circuit. Return a counter of the errors which have been pushed to the front.

Parameters:
  • cliff_circ (Circuit) – Circuit to be simulated. This should be a Clifford circuit.

  • n_rand (int) – Number of random circuit instances, defaults to 1000

Return type:

LogicalErrorDistribution

Returns:

Resulting distribution of errors.

get_error_distribution(optype)[source]

Recovers error model corresponding to particular OpType.

Parameters:

optype (OpType) – OpType for which noise model should be retrieved.

Return type:

ErrorDistribution

Returns:

Error model corresponding to particular OpType.

property noisy_gates: List[OpType]

List of OpTypes with noise.

Returns:

List of OpTypes with noise.

plot()[source]

Generates plot of noise model.

random_propagate(cliff_circ, direction=Direction.backward)[source]

Generate a random noisy instance of the given circuit and propagate the noise forward or backward to recover the logical error.

Parameters:
  • cliff_circ (Circuit) – Circuit to be simulated. This should be a Clifford circuit.

  • direction (Direction) – Direction in which noise should be propagated, defaults to ‘backward’

Raises:

Exception – Raised if direction is invalid.

Return type:

QermitPauli

Returns:

Resulting logical error.

reset_rng(rng)[source]

Reset randomness generator.

Parameters:

rng (Generator) – Randomness generator to be reset to.

scale(scaling_factor)[source]

Generate new error model where all error rates have been scaled by the given scaling factor.

Parameters:

scaling_factor (float) – Factor by which to scale the error rates.

Return type:

NoiseModel

Returns:

New noise model with scaled error rates.

to_dict()[source]

Json serialisable object representing noise model.

Return type:

Dict[str, List[Dict[str, Union[List[int], float]]]]

Returns:

Json serialisable object representing noise model.

class qermit.noise_model.qermit_pauli.QermitPauli(qpt)[source]

For the manipulation of Pauli strings. In particular, how they are changed by the action of Clifford circuits.

Initialised with an initial qubit pauli tensor. Other methods will modify this Qubit Pauli Tensor.

Parameters:

qpt (QubitPauliTensor) – Initial Qubit Pauli Tensor.

apply_circuit(circuit)[source]

Commute the Pauli through the circuit. Given a Clifford circuit C, transform the Pauli P to Q such that PC = CQ.

Parameters:

circuit (Circuit) – Circuit to be applied.

Return type:

None

apply_gate(op, qubits)[source]

Commute the Pauli through the gate. Given a Clifford gate G, transform the Pauli P to Q such that PG = GQ.

Parameters:
  • op (Op) – Operation to commute through.

  • qubits (List[Qubit]) – Qubit on which the operation acts.

Raises:

Exception – Raised if the given gate is not Clifford.

Return type:

None

property circuit: Circuit

Circuit which acts Pauli.

Returns:

Circuit acting Pauli.

property dagger: QermitPauli

Generates the inverse of the Pauli.

Returns:

Conjugate transpose of the Pauli.

get_control_circuit(control_qubit)[source]

Controlled circuit which acts Pauli.

Parameters:

control_qubit (Qubit) – Qubit on which circuit is controlled.

Return type:

Circuit

Returns:

Controlled circuit acting Pauli.

is_measureable(qubit_list)[source]

Checks if this Pauli would be measurable on the given qubits in the computational bases. That is to say if at least one Pauli on the given qubits anticommutes with Z.

Parameters:

qubit_list (List[Qubit]) – Qubits on which if measurable should be checked.

Raises:

Exception – Raised if the given qubits are not contained in this Pauli.

Return type:

bool

Returns:

True if at least one Pauli on the given qubits anticommutes with Z. False otherwise.

post_apply_pauli(pauli, qubit)[source]

Transform Pauli P into QP, where Q is the give pauli.

Parameters:
  • pauli (Pauli) – Pauli to multiply by.

  • qubit (Qubit) – Qubit on which pauli to multiply by should act.

Return type:

None

pre_apply_pauli(pauli, qubit)[source]

Transform Pauli P into PQ, where Q is the give pauli.

Parameters:
  • pauli (Pauli) – Pauli to multiply by.

  • qubit (Qubit) – Qubit on which pauli to multiply by should act.

Return type:

None

property qubit_pauli_tensor: QubitPauliTensor

Current state of the Pauli, following any operations which may have been acted upon it until now.

Returns:

Current state of the Pauli.

reduce_qubits(qubit_list)[source]

Reduces Pauli by removing terms acting on qubits in the given list. A new reduced Pauli is created.

Parameters:

qubit_list (List[Qubit]) – Qubits in Pauli which should be removed.

Return type:

QermitPauli

Returns:

Reduced Pauli.

qermit.noise_model.pauli_error_transpile.PauliErrorTranspile(noise_model)[source]

Generates compiler pass which adds coherent noise to a circuit.

Parameters:

noise_model (NoiseModel) – Model describing the noise to be added. Should be a Pauli noise model.

Return type:

BasePass

Returns:

Compiler pass adding random coherent Pauli noise.

class qermit.noise_model.transpiler_backend.TranspilerBackend(transpiler, result_dict={}, max_batch_size=1000, n_cores=1)[source]

Provides a backend like interface for noise simulation via compiler passes. In particular, for each shot a new circuit is generated by applying the given compiler pass.

Attributes:

transpiler: Compiler pass to apply to simulate noise on a single instance of a circuit. max_batch_size: Shots are simulated in batches. This is the largest shot batch size permitted. result_dict: A dictionary mapping handles to results. backend: Backend used to simulate compiled circuits. n_cores: The number of cores used when simulating shots in parallel.

Initialisation method.

Parameters:
  • transpiler (BasePass) – Compiler to use during noise simulation.

  • max_batch_size (int) – Size of the largest batch of shots, defaults to 1000. The total number of shots is distributed between bathes of size 1000 plus a smaller batch for left over shot. These batches will be distributed to multiple cores.

  • result_dict (Dict[ResultHandle, BackendResult]) – Results dictionary, may be used to store existing results within backend, defaults to {}

  • n_cores (int) – Shots will be taken in parallel. This parameter specifies the number of cores to use. The default is to use one core.

default_compilation_pass(**kwargs)[source]

Return a compiler pass which has no affect on the circuit.

Return type:

BasePass

get_counts(circuit, n_shots, cbits=None)[source]

Generate shots from the given circuit.

Parameters:
  • circuit (Circuit) – Circuit to take shots from.

  • n_shots (int) – Number of shots to take from circuit.

  • cbits (Optional[List[Bit]]) – Classical bits to return shots from, defaults to returning all.

Return type:

Counter[Tuple[int, ...]]

Returns:

Counter detailing shots from circuit.

get_result(handle)[source]

Retrieve result from backend.

Parameters:

handle (ResultHandle) – Handle identifying result.

Return type:

BackendResult

Returns:

Result corresponding to handle.

get_results(handles)[source]

Get the results corresponding to a collection of result handles.

Parameters:

handles (Iterable[ResultHandle]) – A collection of handles to retrieve.

Return type:

List[BackendResult]

Returns:

The results corresponding to the given collection of

process_circuit(circuit, n_shots, **kwargs)[source]

[summary]

Parameters:
  • circuit (Circuit) – Submits circuit to run on noisy backend.

  • n_shots (int) – Number of shots to take from circuit.

Return type:

ResultHandle

Returns:

Handle identifying results in result_dict.

process_circuits(circuits, n_shots)[source]

Processes a collection of circuits by making use multiple calls to process_circuit.

Parameters:
  • circuits (Sequence[Circuit]) – A collection of circuit to run.

  • n_shots (Sequence[int]) – The number of shots which should be taken from each circuit.

Return type:

List[ResultHandle]

Returns:

The result handle for each circuit.

rebase_pass()[source]

Return a compiler pass which has no affect on the circuit.

Return type:

BasePass

run_circuit(circuit, n_shots, **kwargs)[source]

Return results of running one circuit.

Parameters:
  • circuit (Circuit) – Circuit to run

  • n_shots (int) – Number of shots to be taken from circuit.

Return type:

BackendResult

Returns:

Result of running circuit.