# What is the corresponding code for finding the state of a qubit on the Bloch sphere?

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.

Thanks.

• 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? Dec 22, 2019 at 17:38
• @MariiaMykhailova Both cases are intended. Dec 22, 2019 at 18:14

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#.

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$$.

• Thank you so much. Dec 22, 2019 at 20:18