Single qubit expectation values give incorrect results if state is not normalized
- Dominant language
- C++
- Stars
- 264
- Forks
- 76
- Avg merge
- 8d 11h
- Merged PRs (30d)
- 2
Description
**Describe the bug**
The results of the "QubitRegister::ExpectationValueX", "...Y" and "...Z" are incorrect if the state is not normalized.
**To Reproduce**
The error can be seen from the following code snippet:
QubitRegister test(1, "base", 0);
iqs::TinyMatrix IDby2;
IDby2(0,1) = IDby2(1,0) = ComplexDP(0., 0.);
IDby2(0,0) = IDby2(1,1)= ComplexDP(std::sqrt(0.5), 0.);
test. Apply1QubitGate(0, IDby2); // state now has a square norm of 1/2.
test.ApplyPauliX(0); //state is |\psi> = 1/sqrt(2) |1>
assert(test.ExpectationValueZ(0) == -0.5); //value should be -1/2 due to un-normalized state
//actual value: test.ExpectationValueZ(0) == 0.
**Additional context**
I was able to track down the problem. In the expectation value functions, a gate is applied to change the basis to the appropriate Pauli operator, and then the value is calculated via the lines 24, 50 and 72 of "src/qureg_expectval.cpp":
BaseType expectation = 1. - 2.*GetProbability(qubit);
This expression assumes the vector is normalized. In particular 1. -> (state norm)^2 for this to be correct in general.
Contributor guide
Assessment
This issue has not been assessed yet.