When running the following simple quantum program to apply Hadamard gate to a single qubit:
operation ApplyHadamard():()
{
body
{
using (qubits = Qubit[1])
{
H(qubits[0]);
}
}
}
I get the following error:
Microsoft.Quantum.Simulation.Simulators.Exceptions.ReleasedQubitsAreNotInZeroState: 'Released qubits are not in zero state.'
I'm using Microsoft's tutorial, and in it there is no mention of anything else needed to make such a program work.
I appreciate it Q# is still in release mode, but It would be great if anyone finds a solution for this.
The documentation has this information a few sections later, in Working with Qubits.
Target machines expect that qubits are in the |0⟩ state immediately before deallocation, so that they can be reused and offered to other
using
blocks for allocation.
Consider running a program on a quantum computer: the number of qubits is very limited, and you want to reuse the released qubits in other parts of the program. If they are not in zero state by that time, they can potentially be still entangled with the qubits which are not yet released, thus operations you perform on them can affect the state of other parts of the program.
Resetting the qubits to zero state automatically when they go outside the scope of their using
block is dangerous as well: if they were entangled with others, measuring them to reset them can affect the state of the unreleased qubits, and thus change the results of the program - without the developer noticing this.
The requirement that the qubits should be in zero state before they can be released aims to remind the developer to double-check that all necessary information has been properly extracted from the qubits, and that they are not entangled with unreleased qubits any more.
Note that using Reset
or ResetAll
before releasing the qubits is not a hard requirement. For example, in Deutsch-Jozsa algorithm the last step of the algorithm is to measure all qubits except the last one. This means that for each of those qubits you already know that their state is either |0⟩ or |1⟩, and you can apply an X gate to the qubits in |1⟩ state to convert them to |0⟩ without calling Reset
to measure them again. The last qubit is known to be in |-⟩ state, and you can convert it to |0⟩ by applying H and X gates.