qermit.noise_model

class qermit.noise_model.noise_model.ErrorDistribution(distribution, rng=Generator(PCG64) at 0x7F9512A3A960)[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(Z_list, X_list, qubit_list, phase=0)[source]

For the manipulation of Pauli strings. In particular, how they are changed by the action of Clifford circuits. Note that each term in the tensor product of the Paulis should be thought of as: (i)^{phase}X^{X_list}Z^{Z_list}

Initialisation is by a list of qubits, and lists of 0, 1 values indicating that a Z or X operator acts there.

Parameters:
  • Z_list (List[int]) – 0 indicates no Z, 1 indicates Z.

  • X_list (List[int]) – 0 indicates no X, 1 indicates X.

  • qubit_list (List[Qubit]) – List of qubits on which the Pauli acts.

  • phase (int) – Phase as a power of i

CX(control_qubit, target_qubit)[source]

Act CX operation. In particular this transforms the Pauli (i)^{phase}X^{X_liist}Z^{Z_list} to CX(i)^{phase}X^{X_liist}Z^{Z_list}CX^{dagger}.

Parameters:
  • control_qubit (Qubit) – Control qubit of CX gate.

  • target_qubit (Qubit) – Target qubit of CX gate.

H(qubit)[source]

Act H operation. In particular this transforms the Pauli (i)^{phase}X^{X_liist}Z^{Z_list} to H(i)^{phase}X^{X_liist}Z^{Z_list}H^{dagger}.

Parameters:

qubit (Qubit) – Qubit in Pauli on which H is acted.

S(qubit)[source]

Act S operation on the pauli. In particular this transforms the pauli (i)^{phase}X^{X_liist}Z^{Z_list} to (i)^{phase}SX^{X_liist}Z^{Z_list}S^{dagger}.

Parameters:

qubit (Qubit) – Qubit in Pauli onto which S is acted.

apply_circuit(circuit)[source]

Apply a circuit to a pauli. This is to say commute tha Pauli through the circuit. The circuit should be a Clifford circuit.

Parameters:

circuit (Circuit) – Circuit to be applied.

apply_gate(op_type, qubits, **kwargs)[source]

Apply operation of given type to given qubit in the pauli. At present the recognised operation types are H, S, CX, Z, Sdg, X, Y, CZ, SWAP, and Barrier.

Parameters:
  • op_type (OpType) – Type of operator to be applied.

  • qubits (List[Qubit]) – Qubits to which operator is applied.

Raises:

Exception – Raised if operator is not recognised.

property circuit: Circuit

Circuit which acts Pauli.

Returns:

Circuit acting Pauli.

static commute_coeff(pauli_one, pauli_two)[source]

Calculate the coefficient which result from commuting pauli_one past pauli_two. That is to say P_2 P_1 = c P_1 P_2 where c is the coefficient returned by this function.

Parameters:
Raises:

Exception – Raised if the Paulis do not act on matching qubits.

Return type:

int

Returns:

Coefficient resulting from commuting the two Paulis.

dagger()[source]

Generates the inverse of the Pauli.

Return type:

QermitPauli

Returns:

Conjugate transpose of the Pauli.

classmethod from_pauli_iterable(pauli_iterable, qubit_list)[source]

Create a QermitPauli from a Pauli iterable.

Parameters:
  • pauli_iterable (Iterable[Pauli]) – The Pauli iterable to convert.

  • qubit_list (List[Qubit]) – The qubits on which the resulting pauli will act.

Return type:

QermitPauli

Returns:

The pauli corresponding to the given iterable.

classmethod from_qubit_pauli_string(qps)[source]

Create a Pauli from a qubit pauli string.

Parameters:

qps (QubitPauliString) – Qubit pauli string to be converted to a Pauli.

Return type:

QermitPauli

Returns:

Pauli created from qubit pauli string.

get_control_circuit(control_qubit)[source]

Controlled circuit which acts Pauli.

Return type:

Circuit

Returns:

Controlled circuit acting Paulii.

property is_identity: bool

True is the pauli represents the all I string.

Returns:

True is the pauli represents the all I string.

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.

property pauli_string: Tuple[List[Pauli], complex]

List of Paulis which correspond to Pauli, and the phase.

Returns:

[description]

post_apply_X(qubit)[source]

Post-apply X Pauli ito qubit.

Parameters:

qubit (Qubit) – Qubit to which X is post-applied.

post_apply_Z(qubit)[source]

Post-apply Z Pauli ito qubit.

Parameters:

qubit (Qubit) – Qubit to which Z is post-applied.

post_apply_pauli(pauli, qubit)[source]

Post apply a Pauli operation.

Parameters:
Raises:

Exception – Raised if pauli is not a Pauli operation.

pre_apply_X(qubit)[source]

Pre-apply X Pauli ito qubit.

Parameters:

qubit (Qubit) – Qubit to which X is pre-applied.

pre_apply_Z(qubit)[source]

Pre-apply Z Pauli ito qubit.

Parameters:

qubit (Qubit) – Qubit to which Z is pre-applied.

pre_apply_pauli(pauli, qubit)[source]

Pre apply by a pauli on a particular qubit.

Parameters:
Raises:

Exception – Raised if pauli is not a pauli operation.

pre_multiply(pauli)[source]

Pre-multiply by a Pauli.

Parameters:

pauli (QermitPauli) – Pauli to pre multiply by.

property qubit_pauli_string: Tuple[QubitPauliString, complex]

Qubit pauli string corresponding to Pauli, along with the appropriate phase.

Returns:

Pauli string and phase corresponding to Pauli.

classmethod random_pauli(qubit_list, rng=Generator(PCG64) at 0x7F9512A3AA40)[source]

Generates a uniformly random Pauli.

Parameters:
  • qubit_list (List[Qubit]) – Qubits on which the Pauli acts.

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

Return type:

QermitPauli

Returns:

Random pauli.

reduce_qubits(qubit_list)[source]

Reduces Pauli onto given list of qubits. A new reduced Pauli is created.

Parameters:

qubit_list (List[Qubit]) – Qubits onto which pauli should be reduced.

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.