To find the state of a qubit on the Bloch sphere we use the following formula:

\begin{equation} |\psi\rangle=\mathrm{cos}\frac{\theta}{2}|0\rangle+\mathrm{e}^{i\phi}\mathrm{sin}\frac{\theta}{2}|1\rangle \end{equation} Is there any built-in function/operation for that in Q#? If not, how to implement this formula in the Q# code? I'm interested in both preparing a qubit in this state given the two angles, and finding the angles given a qubit in some unknown state.


  • $\begingroup$ Could you please clarify the question? Do you want to prepare a qubit in this state given the two angular parameters, or do you want to find the angles given a qubit in some unknown state? $\endgroup$ Dec 22, 2019 at 17:38
  • $\begingroup$ @MariiaMykhailova Both cases are intended. $\endgroup$
    – Coder
    Dec 22, 2019 at 18:14

1 Answer 1


Prepare a qubit in state $|\psi\rangle=\mathrm{cos}\frac{\theta}{2}|0\rangle+\mathrm{e}^{i\phi}\mathrm{sin}\frac{\theta}{2}|1\rangle$, given the angles $\psi$ and $\theta$.

Let's start with a qubit in the $|0\rangle$ state, as is customary for Q#.

  • You can use one of the general library operations to prepare the state, such as PrepareArbitraryState.
  • Or you can easily do it yourself using a sequence two gates:
    • Ry($\theta$) will transform the qubit from the $|0\rangle$ state to $|\psi\rangle=\cos\frac{\theta}{2}|0\rangle+\sin\frac{\theta}{2}|1\rangle$,
    • R1($\phi$) will further transform that state into the state you need.

Figure out the angles $\psi$ and $\theta$, given a qubit in some unknown state.

This is not possible to do in Q#, unless you're willing to do some hacks that will not work on a quantum device. Since real quantum systems don't allow you to peek into their state to get their exact coefficients, Q# doesn't allow you to do this on language level either.

However, if you're running a program on a full-state simulator, you can work around this and use DumpMachine function to output the qubit state and then analyze it. Here is an example of such output for state $|-i\rangle = \frac{1}{\sqrt2}|0\rangle - \frac{i}{\sqrt2}|1\rangle$:

# wave function for qubits with ids (least to most significant): 0
∣0❭:     0.707107 +  0.000000 i  ==     ***********          [ 0.500000 ]     --- [  0.00000 rad ]
∣1❭:     0.000000 + -0.707107 i  ==     ***********          [ 0.500000 ]    ↓    [ -1.57080 rad ]

Given the complex amplitudes of the state, you can figure out $\psi$ and $\theta$.

  • $\begingroup$ Thank you so much. $\endgroup$
    – Coder
    Dec 22, 2019 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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