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