# QPE example on Qiskit

I am learning to do QPE on qiskit. I wanted to choose a simple example to learn, and hence began with the state $$|+\rangle$$ and operator $$X$$.

I initialized $$|+\rangle$$ this way:

circ = QuantumCircuit(1)

circ.h(0)

a = Custom(num_qubits=1, state='zero', state_vector=None, circuit=circ)

blah = a.construct_circuit(mode='circuit', register=None)

statevec = Statevector.from_instruction(circ).data


I then defined $$X$$ this way:

b = MatrixOperator(np.array([[0,1],[1,0]]), basis=None,
z2_symmetries=None, atol=1e-12, name=None)


Finally, I gave the QPE commands:

backend = Aer.get_backend('statevector_simulator')

qpe = QPE(operator=b, state_in=statevec, iqft=None,
num_time_slices=1, num_ancillae=1,
expansion_mode='trotter', expansion_order=1,
shallow_circuit_concat=False)

quantum_instance = QuantumInstance(backend=backend)

results = qpe.run(quantum_instance)


However, when I try running QPE, I run into this error:

AttributeError: 'numpy.ndarray' object has no attribute 'construct_circuit'


If I define my operator instead this way:

b = Operator([[0,1],[1,0]])


I get the error:

AquaError: "Unsupported type to convert to WeightedPauliOperator: <class 'qiskit.quantum_info.operators.operator.Operator'>"


I also tried:

b = Pauli.x


but ran into:

AttributeError: 'property' object has no attribute 'copy'


Can someone help me with defining the state and operator correctly?

You should try to insert your code so that it is more readable. Take the code in your notebook, and entered it in with the Preformatted text option. This way it is easier to produce the errors you get and make it easier for people to help you to find a solution

In regard to your issue of defining the operator, as I mentioned in another answer sometime earlier, you can define the operator as:


from qiskit.aqua.operators import WeightedPauliOperator

dict = {

'paulis': [{"coeff": {"imag": 0.00, "real": 1 }, "label": "X" }}]
}

b = WeightedPauliOperator.from_dict(dict)



Now you can try to pass b in the QPE(operator = b, , …)

UPDATE:

Try to run this:

import qiskit
from qiskit import __qiskit_version__
from qiskit.aqua.algorithms import QPE
from qiskit.aqua.operators import WeightedPauliOperator
from qiskit.aqua.components.initial_states import Custom
from qiskit import IBMQ, BasicAer, Aer
from qiskit import QuantumCircuit
from qiskit.aqua import QuantumInstance
from qiskit.circuit.library import QFT

print(qiskit.__qiskit_version__)

a = Custom(1,state = 'uniform')  #Initial state as you wanted.
dict = {
'paulis': [{"coeff": {"imag": 0.00, "real": 1 }, "label": "X" }]
}

b = WeightedPauliOperator.from_dict(dict) #Operator for input into QPE

#-------- Setting up Backend --------#
quantum_instance = QuantumInstance(backend=Aer.get_backend("statevector_simulator"), shots=1)
#------------------------------------#

n_ancillae = 3
iqft = QFT(n_ancillae).inverse()
qpe = QPE(b, a, iqft, num_time_slices=1, num_ancillae=n_ancillae,
expansion_mode='trotter', expansion_order=1, shallow_circuit_concat=False)
qpe_result = qpe.run(quantum_instance)

qc = qpe.construct_circuit(measurement = True)
qc.draw( )


Running the above code I get the following:

• Thanks a lot! I'll take into account your comments on adding lines of code in my question. Unfortunately, I still get the error: AttributeError: 'numpy.ndarray' object has no attribute 'construct_circuit'. :-( Oct 15 '20 at 12:45
• Thanks a lot for the update. Unfortunately, this throws an error in the iqft line (I use qiskit19): TypeError: Can't instantiate abstract class QFT with abstract methods _build_circuit, _build_matrix. Oct 15 '20 at 18:12
• Can you try to use the IBM quantum experience jupyter notebook to run the code I posted above? I just ran it and it worked for me. Oct 15 '20 at 18:30
• Thanks a lot! It works on Jupyter notebook, but throws error on spyder, so I'll continue calculations on Jupyter. :-) Thanks again for your help. Oct 16 '20 at 13:19