3

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!

Davit Khachatryan
  • 4,301
  • 1
  • 10
  • 21
Martin
  • 137
  • 4

1 Answers1

2

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.

Ryan Shaffer
  • 548
  • 3
  • 8
  • 1
    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? (https://docs.microsoft.com/en-us/quantum/concepts/pauli-measurements) – Martin May 16 '20 at 12:36
  • 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. – Ryan Shaffer May 16 '20 at 13:51
  • I understand now, thanks a lot for the clear explanation! – Martin May 17 '20 at 12:32