Quantum Computing Asked by srinivasa prasannaa on December 16, 2020
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 --------#
provider = IBMQ.load_account()
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:
Correct answer by KAJ226 on December 16, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP