Download this file as Jupyter notebook: intro.ipynb.

Example: Introduction to the Simulator

True-Q™ offers a highly versatile Simulator that allows users to simulate arbitrary circuits with a wide variety of noise models and options for noise model customization. In particular, the simulator can be used to:

  1. Simulate the final quantum state of a circuit. This will be a pure state vector if the noise model is ideal or has only unitary noise, and will be a density matrix otherwise.

  2. Simulate the total effective operator of a circuit. This will be a unitary matrix if the noise model is ideal or has only unitary noise, and will be a superoperator otherwise.

  3. Sample a given number of bitstrings (or ditstrings if higher energy levels are defined) from the distribution defined by the final simulated quantum state of a circuit. These results can be returned, or can be automatically populated into the respective results attributes of the given circuits.

In this example, we go through the basic features of the True-Q™ simulator. We begin by instantiating a circuit to play with:

import numpy as np
import trueq as tq

circuit = tq.Circuit([{0: tq.Gate.x, 1: tq.Gate.y}, {(0, 1):}])
0 1 Key: Labels: (0,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (1,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: 180.00 -1.00j 1.00j Y Labels: (0, 1) Name: Aliases: Locally Equivalent: CNOT Generators: ZZ: -90.00 ZI: 90.00 IZ: 90.00 1.00 1.00 1.00 -1.00 CZ CZ 1 Labels: (0,) Name: Meas M Labels: (1,) Name: Meas M

Simulator Basics

In its simplest configuration, a (noiseless) simulator can be instantiated from True-Q™’s Simulator class as follows:

sim = tq.Simulator()

We can use it to simulate the final state of the circuit, with all initial states prepared as \(|0\rangle\) by default. Since the simulator is noiseless, the output state is a pure state; a noisy simulator will generally return a density matrix instead.

array([0.+0.j, 0.+0.j, 0.+0.j, 0.-1.j])

If we want to force the output to be a density matrix, we can use:

array([[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.-0.j, 0.-0.j, 0.-0.j, 1.+0.j]])

We can also use it to compute the overall action of the circuit. Since the simulator is currently noiseless, the output is a unitary matrix; a noisy simulator will return a superoperator (in the rowstacked basis).


Finally, we can use a simulator to populate the results attribute of the circuit. Currently, this attribute is just an empty dictionary:

Results({}, dim=None)

But after calling the run() method, it has results that are randomly sampled bitstrings from the final state of a state simulation:

[8]:, n_shots=100)
Results({'11': 100})

In this case, all 100 shots end in the 11 state because the final state is a computational eigenstate.

When re-running the simulation, it will overwrite the existing results unless otherwise specified. Another option is to call the simulator’s sample() method which returns a Results object directly without affecting the circuit’s results attribute:

results = sim.sample(circuit, n_shots=100)
Results({'11': 100})

We can also specify infinite shots to get the expectation values of each bitstring:

[10]:, n_shots=np.inf, overwrite=True)
Results({'11': 1.0})

Adding Noise Sources

We construct a noisy simulator by appending noise sources to a noiseless simulator. The example below demonstrates this using two of True-Q™'s built-in noise sources:

# Add an overrotation noise, which causes single qubit gates to be simulated as U^1.02

# Add a depolarizing noise source at a rate of 0.8% per acted-on qubit per cycle

# Note that noisy simulators can be constructed as one-liners
other_sim = tq.Simulator().add_overrotation(single_sys=0.02).add_depolarizing(p=0.008)


Note that simulation is cycle-based. Each noise source is called to add noise to the quantum state (or to the superoperator if operator() is called) for each cycle in a circuit. The order in which noise sources are applied is dictated by the order in which they were added to the simulator.

Now, since the simulator applies a depolarizing noise source, we get a density operator rather than a pure state:


After calling the run() method (overwriting the results from above), we end up with noisy outcomes:

[13]:, n_shots=100, overwrite=True)
Results({'11': 100})

For more information on both built-in and custom noise sources, check out Example: Advanced Simulator Usage next.


Download this file as Jupyter notebook: intro.ipynb.