3
$\begingroup$

I have a state $|x\rangle$ and I want to get the expected result when measuring observable $A$ (i.e. get the result of $\langle x| A | x \rangle$). In my case $A$ can be any of the Pauli gates. Does anyone know how to do this in Q#? There doesn't seem to be an inner product function in the libraries.

Thanks for the help!

$\endgroup$

1 Answer 1

2
$\begingroup$

It sounds like you're really asking about how to measure in a given Pauli basis. (I'm assuming for simplicity that you are working with only a single qubit, but the below is generalizable to multiple qubits as well.)

Q# has a Measure operation which performs a measurement in a given Pauli basis. For example, if you have a Qubit object q in some state $|\psi\rangle$ and you want to measure it in the $X$ basis, you can write:

   let result = Measure([PauliX], [q]);

Here result will be either Zero or One. If you want to then estimate the expectation value of the measurement, you would need to repeat your full operation (including whatever you did to prepare your qubit in the state $|\psi\rangle$) many times and take an average of the measurement results.

$\endgroup$
3
  • 1
    $\begingroup$ Hi Ryan, thanks for your reply! I tried to measure a qubit which is in the |0> state in the Pauli X basis, but I get an expected value of around 0.5, like if the qubit was in superposition. Shouldn't I get expected value of 0 as that is the result of <0|X|0> ? In the documentation it seems like a Hadamard is applied when trying to measure in the Pauli X basis, so that would explain why I get 0.5, but I don't get why would you apply an H gate? (docs.microsoft.com/en-us/quantum/concepts/pauli-measurements) $\endgroup$
    – Martin
    May 16, 2020 at 12:36
  • $\begingroup$ If your qubit is in the |0⟩ state, that is a Pauli-Z basis state. In terms of Pauli-X basis states, |0⟩ = (|+⟩ + |-⟩)/sqrt(2), so it is actually in superposition with respect to that basis. When measuring a state in the Pauli-X basis, a Zero result corresponds to |+⟩ and a One result corresponds to |-⟩. So it's correct that measuring the state |0⟩ in the Pauli-X basis will give equal probabilities of Zero and One. On the other hand, if you measure |0⟩ in the Pauli-Z basis, you should get Zero with 100% probability. $\endgroup$ May 16, 2020 at 13:51
  • $\begingroup$ I understand now, thanks a lot for the clear explanation! $\endgroup$
    – Martin
    May 17, 2020 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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