I'm very new to quantum mechanics. I'm thinking of writing a quantum computer simulator, would the following work?

  • Each qubit is stored as a single bit,
  • For each operation, the qubits involved are transformed into a complex vector of amplitudes of size 2^N. This will result in a vector containing 2^N-1 0s and one 1.
  • This vector is multiplied by the unitary matrix representing the operation.
  • The resulting vector is squared element-wise.
  • An outcome is picked using the elements of the result as probabilities and the qubits are set according to this outcome.

I'm not concerned with the running time or the memory cost of the algorithm. What I am concerned about is whether this would result in a physically accurate simulation.

  • $\begingroup$ I suspect that it is incorrect and that I don't quite understand what happens when a qubit is measured, but I'd like to know why it is incorrect; can you provide a case where the above algorithm generates an incorrect result. $\endgroup$ Jun 12, 2011 at 8:08

3 Answers 3


Yes, this will essentially work, although there are some pitfalls in the way you've phrased things. For one, you cannot store each qubit as a single bit, but you can work things out so that any computation you do starts from a state which can be represented by $N$ classical bits, i.e. the zero state.

Following this your second and third bullet points are correct.

As for the fourth, element-wise squaring implies that your measurement is in the computational basis. If you need to measure in, say, the Fourier basis, you will need to convert to a computational basis measurement by including a Fourier transformation in step 3.

Bullet 5, like bullet 1, conflates the classical information encoded onto/extracted from qubits with the qubits themselves. Everything here is fine assuming you mean for step 5 to be the absolute end of the computation, but you cannot, in general, loop from step 5 back to step 2.

Ultimately the reason your simulation works is that any computation on $N$ qubits can be represented as a single ($2^N\times 2^N$) unitary acting on the zero state followed by measurement in the computational basis. You cannot, however, use a simulation such as yours to do gate-by-gate simulation of a quantum algorithm.

  • $\begingroup$ Thanks, that's sort of what I expected. Could you elaborate on why it is not possible to do gate-by-gate simulation? $\endgroup$ Jun 12, 2011 at 15:10
  • 2
    $\begingroup$ Sure, consider what happens if you apply 2 sequential Hadamard gates to the zero state and measure the result. A quantum computer will always give the answer zero since $\mathbf{H}\mathbf{H}\vert 0 \rangle = \mathbf{I}\vert 0 \rangle = \vert 0 \rangle$. Using your simulator in a gate-by-gate manner, a measurement would be made after the first Hadamard, the input to the second Hadamard would then be $\vert 1 \rangle$ with probability 1/2 and the final measurement will be of the state $\mathbf{H}\vert 1 \rangle$. This measurement will yield 1 with probability 1/2. $\endgroup$ Jun 12, 2011 at 15:21

No, this won't work. The last step of picking an outcome by squaring the elements of the result and resetting the qubits are set according to this outcome is equivalent to measuring the state of your quantum compute. If you measure the state of your quantum computer after every step, this decoheres any coherent superposition. You will get a computation which can easily be simulated on a classical computer, and which you won't be able to use for any interesting quantum computations. Pretty much any quantum algorithm that takes more than one step will give an example of a case where this won't give the right answer.

  • $\begingroup$ You're certainly correct if Dan intends to iterate through his steps. But is it not the case, as I mentioned in my answer, that the simulation is fine (albeit inefficient) as long as the unitary in step 3 represents the entire computation? $\endgroup$ Jun 12, 2011 at 23:46
  • 1
    $\begingroup$ @John: Good point. I didn't look at your answer closely enough before posting my own, so there is some duplication. But it really sounded to me as if Dan is intending to iterate through his steps. $\endgroup$ Jun 12, 2011 at 23:48
  • $\begingroup$ Shor, Thanks for posting this answer. It does clear a few things up for me. It's a privilege to have a question I asked answered by someone with a name I recognize, (assuming you are the same Peter Shor). $\endgroup$ Jun 13, 2011 at 10:26

The information about an $N$-qubit computer is given by $2^N$ complex numbers. A single operation means to multiply this vector by a unitary matrix which means doing something between $2^N$ and $4^N$ operations (usually the first one because the operations are "localized" on the qubits, so they're not quite generic matrices of the same size).

At the end, you measure the qubits as $N$ ordinary bits. That's when the vector gets reduced to $2^{N}-1$ zeros and $1$ entry equal to one. It's not quite clear from your wording whether you realize that this special form only occurs once, at the very end of the calculation - after you do the "measurement".

The problem in turning this to a practical solution is that $2^N$ operations is a very high number for useful quantum computers that would have at least $N=128$ or much more. A quantum computer would do the operation in one step instead of $10^{38}$ steps. Also, it wouldn't need the classical memory $10^{38+}$ - just 128 spins etc. - to remember all those quantum amplitudes haha.

  • $\begingroup$ I realize that this is the assumed case. I want specifics, why can't it be done as in the question? $\endgroup$ Jun 12, 2011 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.