Hi!
After an intense week of events, this week it’s my turn to go back to the Quantum Computing studio. In this particular case, to a detail that we have to keep in mind when we work with Q #
When a qubit is in an eigenstate of the measurement operator, i.e., either in |0> or in |1>, we need to reset the Qubit before the end of an operation. Qubits need to be in |0> when they are released, so we have to manually reset the qubit before releasing it using the Z() operation if needed.
Well, let’s take as a basis one in which we initialize a Qubit and apply an operation Hadamard operation as shown in the following code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
operation HGate (initial: Result) : (Result) | |
{ | |
body | |
{ | |
mutable res = Zero; | |
using (qubits = Qubit[1]) | |
{ | |
let qubit = qubits[0]; | |
H(qubit); | |
set res = M (qubit); | |
if (res == One) | |
{ | |
X(qubit); | |
} | |
} | |
return (res); | |
} | |
} |
In line 10 I store the state of the Qubit in the return variable and then on lines 11 to 14 verify that the state of the same is Zero. If not, I need to reset the Qubit to Zero before the end of the operation on line 13.
Well, this code works, and that’s amazing! However when we use the Microsoft Quantum DevKit we also have an operation that allows us to obtain the status of a Qubit and release it in a single operation. This operation is MResetZ(). The same previous code using this operation it is much more nice to read
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
operation HGateUsingMResetZ (initial: Result) : (Result) | |
{ | |
body | |
{ | |
mutable res = Zero; | |
using (qubits = Qubit[1]) | |
{ | |
let qubit = qubits[0]; | |
H(qubit); | |
set res = MResetZ(qubit); | |
} | |
return (res); | |
} | |
} |
As we can see, the main change is in line 10 where I assign the state of the Qubit to the return variable and also reset the Qubit to Zero.
Finally, from a Console App I invoke both operations and the result is equal.
GitHub Source Code link
Happy QCoding!
Greetings @ Toronto
El Bruno
References
- Microsoft Quantum Development Kit
- Q# Reference, M operation
- Q# Reference, H operation
- Q# Reference, MResetZ operation
My Posts
- El Bruno, Qubit operations in Q#
- El Bruno, Information about Simulators on Microsoft Quantum Development Kit (Do you have 16 TB RAM?)
- El Bruno, Hello QWorld using Microsoft #Quantum Development Kit (fix to build the project) [Updated]
- El Bruno, More information about the Microsoft bet in Quantum Computing
- El Bruno, Quantum Computing, let’s add this one to Artificial Intelligence and Mixed Reality!
10 comments