HI!

Yesterday I wrote on the MResetZ() which allows us to get the status of a Qubit and release it. Well, at the end of each Q# operation, all the used Qubits need to be released for the simulator to work properly.

The following sample code will show a bad sample where a Qubit is used and not released correctly:


operation HGateWithOutReset (initial: Result) : (Result)
{
body
{
mutable res = Zero;
using (qubits = Qubit[2])
{
let qubit1 = qubits[0];
let qubit2 = qubits[1];
H(qubit1);
set res = M(qubit1);
}
return (res);
}
}

So, in runtime we will find this amazing and cool error:

I1

Microsoft.Quantum.Simulation.Simulators.Exceptions.ReleasedQubitsAreNotInZeroState

  HResult=0x80131500

  Message=Released qubits are not in zero state.

  Source=Microsoft.Quantum.Simulation.Simulators

Microsoft Quantum Development Kit offers us a series of operations that help us to release Qubits: Reset() and ResetAll(). As its name indicates Reset() is intended to release a single Qubit and ResetAll() to release an array of Qubits.

Given a single Qubit, Reset() evaluates the Qubit state and ensures that the Qubit is in the state |0⟩ so it can be safely released.

The following 2 operations show examples of how to use these operations

 


operation HGateWithResetEachQubit (initial: Result) : (Result)
{
body
{
mutable res = Zero;
using (qubits = Qubit[2])
{
let qubit1 = qubits[0];
let qubit2 = qubits[1];
H(qubit1);
set res = M(qubit1);
Reset(qubit1);
Reset(qubit2);
}
return (res);
}
}
operation HGateWithResetAllQubits (initial: Result) : (Result)
{
body
{
mutable res = Zero;
using (qubits = Qubit[2])
{
let qubit1 = qubits[0];
let qubit2 = qubits[1];
H(qubit1);
set res = M(qubit1);
ResetAll(qubits);
}
return (res);
}
}

Happy QCoding!

Greetings @ Toronto

El Bruno

References

My Posts

 

9 responses to “#VS2017 – Releasing #Qubits using Reset() and ResetAll() in Q# on Microsoft Quantum Development Kit”

  1. […] El Bruno, Releasing Qubits using Reset() and ResetAll() in Q# […]

    Like

  2. […] El Bruno, Releasing Qubits using Reset() and ResetAll() in Q# […]

    Like

  3. […] El Bruno, Releasing Qubits using Reset() and ResetAll() in Q# […]

    Like

  4. […] El Bruno, Releasing Qubits using Reset() and ResetAll() in Q# […]

    Like

  5. […] El Bruno, Releasing Qubits using Reset() and ResetAll() in Q# […]

    Like

  6. […] El Bruno, Releasing Qubits using Reset() and ResetAll() in Q# […]

    Like

Leave a reply to #Quantum – The key for Mac and Linux is on Visual Studio Code #VSCode – El Bruno Cancel reply

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading