# Example: State Preparation and Measurement Noise

:

import trueq as tq
import numpy as np


The most convenient way to add measurement noise is through the add_readout_error() noise source. There are several ways to specify readout error with this method. Measurement noise is applied only when a simulator’s sample() or run() method is called.

The most basic method is to provide a single number. In the following example, each qubit will get a (symmetric) 1% bitflip error.

:

sim = tq.Simulator().add_readout_error(0.01)
circuit = tq.Circuit({range(5): tq.Meas()})
sim.run(circuit, n_shots=1000)
circuit.results

:

Results({'00000': 964, '00001': 10, '00010': 8, '00100': 4, '01000': 6, '01001': 1, '10000': 7})


We can use a dictionary to specify the readout error of particular qubits. Qubits that are not explicitly assigned a readout error get no noise, or the specified default value. In this example, all qubits get 1% readout error, except qubit 1 gets a 50% readout error.

:

sim = tq.Simulator().add_readout_error(0.01, {1: 0.5})
circuit = tq.Circuit({range(5): tq.Meas()})
sim.run(circuit, n_shots=1000)
circuit.results

:

Results({'00000': 474, '00001': 5, '00010': 9, '00011': 1, '00100': 6, '10000': 2, '10010': 1, '01000': 475, '01001': 6, '01010': 8, '01011': 1, '01100': 5, '11000': 7})


In both of the above examples, any probability can be replaced with a pair of probabilities [p10, p01], where p10 is the probability of flipping a ‘0’ to a ‘1’ right before measurement, and p01 is the probability of flipping a ‘1’ to a ‘0’ right before measurement. In the following example, qubits get 1% readout error, except qubit 1 gets 5% readout error, and qubit 3 get asymmetric readout error of 1% on $$|0\rangle$$ and 7% on $$|1\rangle$$.

:

sim = tq.Simulator().add_readout_error(0.01, {1: 0.05, 3: [0.01, 0.07]})
circuit = tq.Circuit({range(5): tq.Meas()})
sim.run(circuit, n_shots=1000)
circuit.results

:

Results({'00000': 892, '00001': 16, '00100': 18, '00101': 1, '10000': 11, '00010': 10, '00110': 1, '01000': 50, '11000': 1})


## Specifying Confusion Matrices

We can also use the full confusion matrix anywhere in the add_readout_error() noise source. There isn’t any value to this for a single qubit confusion matrix because it is fully specified by a length-2 vector as described above, but if we are interested in adding misclassification into a third level, or correlated readout error, then a confusion matrix gives us control over all the probabilities. In the following example, we specify that 0 and 1 are misclassified as 2 with a small probability.

:

sim = tq.Simulator().add_readout_error([[0.98, 0.06], [0.01, 0.9], [0.01, 0.04]])
circuit = tq.Circuit({range(5): tq.Meas()})
sim.run(circuit, n_shots=1000)
circuit.results

:

Results({'00000': 916, '00001': 12, '00002': 8, '00010': 10, '00020': 8, '00100': 12, '00200': 4, '01000': 5, '02000': 6, '10000': 6, '10010': 1, '10020': 1, '20000': 11}, dim=3)


Similarly, in the following example, we specify a correlated readout error on qubits 2 and 3, where the rows and columns are in the usual 00, 01, 10, 11 order.

:

confusion_mat = [
[0.99, 0.05, 0.00, 0.00],
[0.00, 0.90, 0.00, 0.02],
[0.00, 0.05, 0.96, 0.00],
[0.01, 0.00, 0.04, 0.98],
]
circuit = tq.Circuit({range(5): tq.Meas()})
sim.run(circuit, n_shots=10000)
circuit.results

:

Results({'00000': 9904, '00110': 96})


Finally, as a more advanced option, measurement error can also be specified as a positive-operator valued measurement (POVM).

As a quick reminder (look elsewhere for a full description, e.g. Wikipedia or the lecture notes of John Watrous), a POVM is a set of positive-semi-definite matrices $$\{P_i\}_{i=1}^N$$ that sum to the identity matrix, $$\sum_{i=1}^N P_i = \mathbb{I}$$. The probability of observing the outcome $$i$$ when measuring a state $$\rho$$ is equal to $$p_i:=\operatorname{Tr}(\rho P_i)$$. One can also take a POVM defined on a single qubit, and tensor it together to get a POVM on a collection of qubits. For example, if the POVM above describes measurement on a single qubit, the probability of measuring the outcomes $$(i_0, i_1, i_2)$$ on three qubits is simply $$p_{i_0}p_{i_1}p_{i_2}$$. This is why we use the Tensor object as a primitive for specifying POVMs; its purpose is to store tensor project structures sparsely without actually taking kronecker products between subsystems unless necessary.

The add_readout_error() supports POVMs in addition to all the formats described in the previous section. A POVM is specified a 3D array where the first index $$i$$ ranges over POVM elements $$P_i$$.

In this example, we construct a coherent measurement error on qubit 3.

:

# Define a set of ideal POVM operators for each subsystem
proj0 = np.array([[1, 0], [0, 0]])  # project onto this one to get a "0"
proj1 = np.array([[0, 0], [0, 1]])  # project onto this one to get a "1"
ideal = np.array([proj0, proj1])

# Define a unitary rotation about X by 20 degrees
u = tq.Gate.from_generators("X", 20).mat

# Define noisy POVM operators by rotating the ideal POVM operators by U, which
# coherently changes the basis of the measurement
twisted_povm = [u @ x @ u.conj().T for x in ideal]

# initialize a simulator with the above POVM applied to qubit 3
circuit = tq.Circuit({range(5): tq.Meas()})
sim.run(circuit, n_shots=1000)
circuit.results

:

Results({'00000': 965, '00010': 35})


In this example, we add a third measurement label even though the simulation is on qubits, so that the outcomes ‘0’, ‘1’, and ‘2’ are all possible.

Here we specify that the state $$|0\rangle$$ results in the outcome ‘0’ 99% of the time, but also results in the outcome ‘2’ 1% of the time. Similarly, the state $$|1\rangle>$$ results in the outcome ‘1’ 70% of the time, but also results in ‘2’ 25% of the time and ‘0’ 5% of the time. This particular example could be more efficiently implemented as a confusion matrix, but is presented as a POVM for demonstration.

:

povm = [np.diag([0.99, 0.05]), np.diag([0, 0.7]), np.diag([0.01, 0.25])]
circuit = tq.Circuit([{1: tq.Gate.h}, {(0, 1): tq.Meas()}])
sim.run(circuit, n_shots=10000)
circuit.results

:

Results({'00': 5114, '20': 45, '01': 3456, '21': 38, '02': 1337, '22': 10}, dim=3)


State preparation noise can be added using the add_prep() noise source.

Note

The add_prep() noise source only takes place when a Prep() object is encountered. Many circuits do not have any Prep() objects unless explicitly added.

We can either enter the noise as the probability of a bitflip during preparation of $$|0\rangle$$:

:

sim = tq.Simulator().add_prep(0.01)
circuit = tq.Circuit([{0: tq.Prep()}])
tq.plot_mat(sim.state(circuit).mat()) Or we can specify the pure state or density matrix we want to prepare with. Here a density matrix is used:

:

sim = tq.Simulator().add_prep([[0.75, 0], [0, 0.25]])
circuit = tq.Circuit([{0: tq.Prep()}])
tq.plot_mat(sim.state(circuit).mat()) We can place different preparations on different qubits. Here, we have a 1% preparation bitflip error by default, but a 20 degree rotation error on qubit 3.

:

u = tq.Gate.from_generators("Y", 20).mat
sim = tq.Simulator().add_prep(0.01, {3: u @ np.diag([1, 0]) @ u.conj().T})
circuit = tq.Circuit([{0: tq.Prep(), 3: tq.Prep()}])
tq.plot_mat(sim.state(circuit).mat()) We can specify preparation states of larger dimension to add leakage levels.

:

sim = tq.Simulator().add_prep(np.diag([0.97, 0.02, 0.01]))
circuit = tq.Circuit([{0: tq.Prep()}, {0: tq.Gate.x}])
tq.plot_mat(sim.state(circuit).mat())
` 