1
$\begingroup$

Introduction

I am using BQSKIT to compile an approximation of a Toffoli gate (for testing purposes) and output to a QISKIT QuantumCircuit. I want to find the expected value of this approximation to find out how good it is using exact methods. In an attempt to do so, I first use this circuit to get an Operator. I then create a state vector and use the expectation_value method to compute the expected value. I include the relevant code snippet below. The result is a complex number: (1.0000000000000004-6.814549413037705e-13j).

The Code

from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
from qiskit.quantum_info import Operator

qc = bqskit_to_qiskit(out_circuit)
op = Operator( qc )

testVector = Statevector( QuantumCircuit( 3 ))
print('Exact:', testVector.expectation_value(op) )

The Primary Question

  1. What is the meaning of a complex expected value?

The Secondary Questions

  1. Am I using expectation_value correctly?
  2. Have I correctly understood that it gives an exact solution via linear algebra and is not based on simulation?
$\endgroup$
1
  • 2
    $\begingroup$ I suspect this is just numerical error in python! $\endgroup$
    – DaftWullie
    Commented Feb 5 at 7:43

1 Answer 1

2
$\begingroup$

This is a floating point error. The way computers work with floating point numbers, very small errors can happen. You can check it by trying to see if 0.1+0.1+0.1 == 0.3 (spoilers, it's not, according to python, 0.1+0.1+0.1=0.30000000000000004)

This is what has happened here. If you were doing the calculation analytically by hand somewhere you would find a place where all the complex elements cancel out. But during this subtraction python has made an error, thus giving you this answer. This error is exactly because it's trying to use linear algebra and it's not a simulation.

I suggest using numpy.real_if_close before the result. This will identify any such errors (like your 0.00000000000068j off answer) and hide them.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.