Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expectation value of Y and variances are wrong in SnapshotExpectationValue #895

Closed
ikkoham opened this issue Aug 18, 2020 · 5 comments
Closed
Labels
bug Something isn't working

Comments

@ikkoham
Copy link
Collaborator

ikkoham commented Aug 18, 2020

Informations

  • Qiskit Aer version: master
  • Python version: 3.8.5
  • Operating system: macOS

What is the current behavior?

from qiskit import QuantumCircuit, execute, Aer
from qiskit.providers.aer.extensions import SnapshotExpectationValue

qc = QuantumCircuit(1)
qc.snapshot_expectation_value('x', [(1, 'X')], qubits=[0], variance=True)
qc.snapshot_expectation_value('y', [(1, 'Y')], qubits=[0], variance=True)
qc.snapshot_expectation_value('z', [(1, 'Z')], qubits=[0], variance=True)

result = execute(qc, Aer.get_backend('qasm_simulator'), shots=1024).result()
print(result.data()['snapshots'])

The output is

{'expectation_value': {'x': [{'value': 0j, 'variance': 0j}],
  'y': [{'value': (1+0j), 'variance': 0j}],
  'z': [{'value': (1+0j), 'variance': 0j}]}}

Are these behaviors as expected? I'm concerned about the following points:

  1. The expectation value of Y is 1, but should be 0.
  2. All variances are 0.
@ikkoham ikkoham added the bug Something isn't working label Aug 18, 2020
@ikkoham ikkoham changed the title Question about SnapshotExpectationValue Expectation value of Y and variances are wrong in SnapshotExpectationValue Aug 19, 2020
@ikkoham
Copy link
Collaborator Author

ikkoham commented Aug 19, 2020

I just found the expectaiton value of Y is correct for statevector_simulator.

from qiskit import QuantumCircuit, execute, Aer

qc = QuantumCircuit(1)
qc.snapshot_expectation_value('x', [(1, 'X')], qubits=[0], variance=True)
qc.snapshot_expectation_value('y', [(1, 'Y')], qubits=[0], variance=True)
qc.snapshot_expectation_value('z', [(1, 'Z')], qubits=[0], variance=True)

result = execute(qc, Aer.get_backend('statevector_simulator')).result()
print(result.data())

the output is

{'snapshots': {'expectation_value': {
'x': [{'value': 0j, 'variance': 0j}], 
'y': [{'value': 0j, 'variance': 0j}],
'z': [{'value': (1+0j), 'variance': 0j}]}},
'statevector': array([1.+0.j, 0.+0.j])}

I have checked this for other input states.

However, variances are still zero.

@yaelbh
Copy link
Contributor

yaelbh commented Aug 19, 2020

Value: I guess that there is a bug in the stabilizer simulator. In the previous execution you were probably automatically directed to the stabilizer simulator, whereas in the second execution you explicitly asked for the non-stabilizer simulator.

Variance: as long as there is no noise it is expected to be 0, because we compute <psi|Y|psi> (we don't perform average of measurement results).

@ikkoham
Copy link
Collaborator Author

ikkoham commented Aug 19, 2020

Thank you for the comment.

I checked about value.

from qiskit import QuantumCircuit, execute, Aer

qc = QuantumCircuit(1)
qc.t(0)
qc.snapshot_expectation_value('x', [(1, 'X')], qubits=[0], variance=True)
qc.snapshot_expectation_value('y', [(1, 'Y')], qubits=[0], variance=True)
qc.snapshot_expectation_value('z', [(1, 'Z')], qubits=[0], variance=True)

result = execute(qc, Aer.get_backend('qasm_simulator')).result()
print(result.data()['snapshots'])

the output is

{'expectation_value': {'x': [{'value': 0j, 'variance': 0j}],
  'y': [{'value': 0j, 'variance': 0j}],
  'z': [{'value': (1+0j), 'variance': 0j}]}}

so I obtained correct value. Your guess seems correct.

For variance, we should return the population variance for (at least) the statevector simulator.
This is a non-zero value. (because <X^2>-<X>^2 = 1 - <X>^2 ≠ 0 in general)

@chriseclectic
Copy link
Member

The variance in snapshots is only the sample variance over noise for each shot of the snapshotted value: eg for 5-shots you have shot_values = [val0, val1, val2, val3, val4], then the returned quantity is mean(shot_values), variance(shot_values) and is (usually) 0 for ideal simulations since the values are all identical. This is somewhat confusing so I am planning on removing it and just returning the mean for average snapshot types.

@ikkoham
Copy link
Collaborator Author

ikkoham commented Sep 3, 2020

The first issue was fixed in #849 .

For the second issue, I discussed with @chriseclectic , and it's good to remove the variance parameter and my proposed variance should be done in other instructions.
So I think this issue is closable.

Thanks a lot.

@ikkoham ikkoham closed this as completed Sep 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants