# How to run an approximate simulation, and why

I'm looking at the Qiskit getting started documentation, https://qiskit.org/documentation/getting_started.html. They create a quantum circuit and run an approximate simulation on it 1000 times: 1. Why bother with an approximate solution when you can get an exact solution? In this case, you can simulate everything with matrices and get this result: $$\begin{bmatrix}{1 \over {\sqrt 2}} \\ 0 \\ 0 \\ {1 \over {\sqrt 2}}\end{bmatrix}$$

From this you know the exact probabilities of each output: 00 occurs $$1 \over 2$$ the time, 11 occurs $$1 \over 2$$ the time. Is an exact simulator available in Qiskit?

1. How does an approximate simulator work? For instance, after passing through the Hadamard gate, we have the qubit

$$\begin{bmatrix} {1 \over {\sqrt 2}} \\ {1 \over {\sqrt 2}} \end{bmatrix}$$

At that point, does the simulator immediately collapse the qubit to $$|0\rangle$$ or $$|1\rangle$$ to avoid potential superpositions? Or would that not be sufficient, and an approximate simulator is more complicated?

• We will keep track of the whole system states via the density matrix, allowing for superpositions of states, applying the gates as tensor products, which will effectively give us one unitary acting on the initial density matrix, from this the probability of measuring the final product states, using a measure M, can be read off as the simulated outputs. Jun 7 '20 at 18:25
• That's kind of my point - if the simulator already has the probabilities of the output, why does it have to sample 1000 times? If I know there's a 50% chance, why do I have to flip a coin? Jun 8 '20 at 7:40
• Remember these are just basic examples, but simulation is important to test other factors such as circuit depths, decoherence, and error correction codes. It's kind of like asking why do we even have random number generators if we know the probability distributions any way, well because a lot more complex applications rely on experimentation. Jun 8 '20 at 14:57

Why bother with an approximate solution when you can get an exact solution?

The reason to have an 'approximate' simulation rather than an exact simulation (and result) is that it more closely resembles our understanding of and interaction with a real quantum computer.

In a real quantum computer, the state of the qubits before measurement is indeed the exact superposition, but retrieving information from this system will always be done through measurements, and those measurements cannot give the full system state - they will only give one bit of information per measurement. Repeated experiments + measurements then allows us to approximate the exact state we believe the system to be in.

Is an exact simulator available in Qiskit?

Qiskit can indeed handle exact simulations - there is the 'statevector_simulator' which will do this; you can retrieve it with aer.get_backend('statevector_simulator'). Note that this does not allow for density matrix simulations, so the methods to simulate noise and error are severely limited. The statevector can then be retrieved from the results object using the .get_statevector() method. See also this webpage from IBM/qiskit.

How does an approximate simulator work?

The simulations do not collapse the superpositions before the actual measurements - if the superpositions were collapsed in an intermediary state of the circuit, there can be no simulations of all things that make a quantum computer 'quantum' - entanglement, interference etc.

For simplicity, if we assume the measurement to take place at the very end of the circuit, just before the measurement the entire state of the system is known (which would be a $$2^{n} \times 2^{n}$$ density matrix; however you can use various insights and tricks to reduce the memory usage. It will always be exponential though.)

A measurement is then a random draw from the distribution of possible measurement outcomes, weighted to their probabilities. For a statevector, this is of course the $$|\alpha|$$'s and $$|\beta|$$'s. For density matrices, it is a bit more intricate as you use projection matrices.

• Thanks for the response. It sounds like real quantum circuits have noise, so qiskit by default adds a realistic amount of noise to the simulation. I tried running statevector_simulator, and usually I got {'00': 2, '11': 2}. That makes sense. But occasionally I got {'11': 1, '00': 3}! What's going on? I thought the statevector_simulator was supposed to be exact, zero noise. I would expect it to output something like {'00': 0.5, '11': 0.5} every time. Jun 10 '20 at 6:47
• You're welcome! Using the .counts() method of the result object will still draw from the probability distribution based on the state vector. The .get_statevector() method (of the results object) will return the proper statevector, if you have used the statevector simulator. See also: qiskit.org/textbook/ch-states/representing-qubit-states.html; section 1.3. I've also updated the answer with this information.
– JSdJ
Jun 10 '20 at 7:18
• Thanks, that worked! I've added the exact solution as another answer. Jun 11 '20 at 16:22

Here's how you can use statevector_simulator to get an exact answer:

import numpy as np
from qiskit import QuantumCircuit, execute, Aer
from qiskit.visualization import plot_histogram

# Use Aer's statevector_simulator
simulator = Aer.get_backend("statevector_simulator")

# Create a Quantum Circuit acting on the q register
circuit = QuantumCircuit(2, 2)

# Add a H gate on qubit 0
circuit.h(0)

# Add a CX (CNOT) gate on control qubit 0 and target qubit 1
circuit.cx(0, 1)

# Execute the circuit on the qasm simulator
job = execute(circuit, simulator)

# Grab results from the job
result = job.result()

out_state = result.get_statevector()
print([abs(q ** 2) for q in out_state])


Running this prints:

[0.5000000000000001, 0.0, 0.0, 0.4999999999999999]


So 00 occurred 50% of the time, and 11 occurred 50% of the time. The minor error is due to floating point rounding.