# How to measure in an arbitrary basis in Q#?

Q# has a measurement operator defined as follows according to the docs:

operation Measure (bases : Pauli[], qubits : Qubit[]) : Result

Where you give a Pauli gate $\{I, \sigma_x, \sigma_y, \sigma_z\}$ as a measurement operator and the qbit is projected onto its eigenvectors.

How can I measure in an arbitrary basis? I know conceptually what I have to do is first rotate my state vector by some amount then measure in one of the Pauli bases, but what are the actual primitive gates and parameters I would use to do that? For example, say I wanted to measure in the following basis:

$\begin{bmatrix} \frac{\sqrt{3}}{2} \\ \frac 1 2 \end{bmatrix}, \begin{bmatrix} \frac{-1}{2} \\ \frac{\sqrt{3}}{2} \end{bmatrix}$

So basically the computational basis but rotated $\pi/6$ radians counter-clockwise around the unit circle.

Let's say you want to distinguish two states:

$$|A\rangle = \cos \alpha |0\rangle + \sin \alpha |1\rangle \\ |B\rangle = -\sin \alpha |0\rangle + \cos \alpha |1\rangle$$

For your particular example $\cos \alpha = \frac {\sqrt{3}}{2}$ and $\sin \alpha = \frac{1}{2}$, so $\alpha = \frac{\pi}{6}$.

These states are orthogonal and can be obtained from $|0\rangle$ and $|1\rangle$ by rotating around Y axis, i.e. by applying Ry(2.0 * alpha, _). You can verify it using the definition of the Ry operation and matrix exponentiation.

Thus, one way to measure the states $|A\rangle$ and $|B\rangle$ is to apply adjoint operation Ry(-2.0 * alpha, _) to your qubit to get those states back to $|0\rangle$ and $|1\rangle$, and then to measure the qubit in the computational basis using operation M (or Measure([PauliZ], _)).

In more general case you'd use other rotation operations (Rx and Rz) to perform the exact rotation which converts your basis states to computational basis before measuring them.

This question is effectively task 1.3 from the Measurements quantum kata, if you want to practice it in Q# (as well as more advanced measurement scenarios).