0
$\begingroup$

Is there a corresponding function that has the same function as function get_expectation_value() in Project Q?

Get the expectation value of qubit_operator w.r.t. the current wave function representeenter preformatted text hered by the supplied quantum register.

Args:

  • qubit_operator: Operator to measure.
  • qureg: Quantum bits to measure.

Returns: Expectation value

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, Aer
import numpy as np
from qiskit.aqua import QuantumInstance
from qiskit.aqua.operators import PauliExpectation, CircuitSampler, StateFn, CircuitOp, CircuitStateFn

qctl = QuantumRegister(10)
psi = QuantumCircuit(qctl)
psi.h(0)
psi = CircuitStateFn(psi)

qctl = QuantumRegister(10)
op = QuantumCircuit(qctl)
op.z(0)
op.ry(np.pi/4,0)
op = CircuitOp(op)

backend = Aer.get_backend('qasm_simulator')
q_instance = QuantumInstance(backend, shots=1024)
measurable_expression = StateFn(op, is_measurement=True).compose(psi)
expectation = PauliExpectation().convert(measurable_expression)
sampler = CircuitSampler(q_instance).convert(expectation)
print('Expectation Value = ', sampler.eval())
$\endgroup$
4
  • 1
    $\begingroup$ Hi @nautilius! It is often a bad idea to try to translate verbatim from one framework to another as it often leads to strange code that does not follow the framework "philosophy". Could you provide an example (code) in Project-Q language of what you are trying to do? Or at least tell if you want to compute this using a quantum computer (so you need the quantum circuits) or if you just want the result and linear algebra is fine. $\endgroup$ – Adrien Suau Jul 5 at 12:07
  • $\begingroup$ Thank you for your answer!I'm trying to get the value of <phi| operator |phi> in Qiskit with the corresponding Sample functions the same as in projectQ, but there is a problem that when I use CircuitSampler to calculate the expectations, it's really slow, espacially when the target circuit include 5~10 qubits, it usually takes a few seconds to calculate. Is there any optimization tricks or other more direct way that I can use to accerlarate the process? $\endgroup$ – nautilius Jul 6 at 2:38
  • $\begingroup$ Can you edit your question to add the problematic code (that is really slow)? Be sure to include everything that is needed in order for others to be able to replicate your results (a minimal working example if you are familiar with other SEs). $\endgroup$ – Adrien Suau Jul 6 at 10:27
  • $\begingroup$ ok, a small example is like above, although not all qubits of the register are used there, but my project will involve circuits of this scale $\endgroup$ – nautilius Jul 8 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.