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

Cannot get expectation value of list ops or composed ops #7743

Closed
mikevitz opened this issue Mar 7, 2022 · 4 comments
Closed

Cannot get expectation value of list ops or composed ops #7743

mikevitz opened this issue Mar 7, 2022 · 4 comments
Labels
bug Something isn't working mod: opflow Related to the Opflow module

Comments

@mikevitz
Copy link

mikevitz commented Mar 7, 2022

Environment

  • Qiskit Terra version:
  • Python version:
  • Operating system:

'qiskit-terra': '0.19.1',
'qiskit-aer': '0.10.2',
'qiskit': '0.34.1',
Python 3.8.12

OS=Amazon Linux, version 2
ID_LIKE="centos rhel fedora"

What is happening?

I am having trouble evaluating operators built from composed ops and list ops. I keep getting the error:
ValueError: Cannot compute overlap with StateFn or Operator if not Measurement. Try taking sf.adjoint() first to convert to measurement.

How can we reproduce the issue?

Copy/paste and run the code block below:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

from qiskit import Aer, QuantumCircuit
from qiskit.circuit import Parameter
from qiskit.opflow import StateFn, PauliSumOp, AerPauliExpectation, ListOp, Gradient
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit.opflow import (StateFn, Zero, One, Plus, Minus, H,
                           DictStateFn, VectorStateFn, CircuitStateFn, OperatorStateFn)

######### construct the parametrized circuit
qc = QuantumCircuit(2)

qc.h(0)
qc.h(1)
qc.cx(0,1)

param1 = Parameter('θ1')
param2 = Parameter('θ2')
qc.rx(param1, 0)
qc.rx(param2, 1)

qc_sfn = StateFn(qc)

######### construct hamiltonians
H_11 = StateFn(PauliSumOp.from_list([("ZI", 1.0)]))
H_12 = StateFn(PauliSumOp.from_list([("IZ", 1.0)]))

######### combine hamiltonian and circuit 
op_11 = ~H_11 @ qc_sfn
op_12 = ~H_12 @ qc_sfn
ops1 = ListOp([op_11, op_12])

print(op_11.to_circuit_op())
print(ops1.to_circuit_op())

op_11.bind_parameters({param1: 0.832619845547938,
                       param2: 0.07103605819788694})

ops1.bind_parameters({param1: 0.832619845547938,
                    param2: 0.07103605819788694})

######## DOESN'T WORK
op_11.eval(One^2)

######## ALSO DOESN'T WORK
ops1.eval(One^2)

What should happen?

Should be able to get the expectation value of the operator.

Any suggestions?

No response

@mikevitz mikevitz added the bug Something isn't working label Mar 7, 2022
@woodsp-ibm woodsp-ibm added the mod: opflow Related to the Opflow module label Mar 7, 2022
@Cryoris
Copy link
Contributor

Cryoris commented Mar 8, 2022

I think you're not using the opflow instructions correctly here:

  • bind_parameters is not an in-place operations, but returns the bounds object
  • your op_11 and ops1 objects are already fully contracted, you can just call eval() on them without any argument

If you change the code as follows it seems to work:

# ... as you had it
bound_11 = op_11.bind_parameters({param1: 0.832619845547938,
                                  param2: 0.07103605819788694})

bounds1 = ops1.bind_parameters({param1: 0.832619845547938,
                               param2: 0.07103605819788694})

print(bound_11.eval())
print(bound1.eval())

@mikevitz
Copy link
Author

mikevitz commented Mar 10, 2022

Thank you for the observation! I see now how to do this.

But I keep getting unexpected results! For example for the code below:

bound = ops1.bind_parameters({param1: 10.832619845547938,  param2: 1.07103605819788694})

print(bound.eval())

No matter what parameters I use, it always gives results:
[0j, 0j]

My understanding is that the code creates a circuit, puts the qubits in superposition via Hadamard, entangles them via CNOT and rotates around x axis in Bloch sphere.

Shouldn't I get non-zero results here?

Thanks again!

@Cryoris
Copy link
Contributor

Cryoris commented Mar 15, 2022

Hmm I think the expectation value is correctly 0 as the Pauli Z operator you measure against introduces a phaseflip and makes the states orthogonal. To get a non-zero result, try rotating the qubit states e.g. around the Y-axis with the QuantumCircuit.ry method.

@jakelishman
Copy link
Member

Since opflow was removed in #11111 and no longer supported, I think this issue is now obsolete. Thanks for the discussion, and feel free to re-open if I've missed anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: opflow Related to the Opflow module
Projects
None yet
Development

No branches or pull requests

4 participants