I am trying to generate a Quantum Phase Estimation (QPE) circuit in QISKIT the following way.

1 - First, I generate a QPE circuit with the following code:

### building inverse quantum fourier transform circuit
iqft = StandardIQFTS(2)
iqft_circ = QuantumCircuit(QuantumRegister(2))
iqft.construct_circuit(mode='circuit', qubits=iqft_circ.qubits, circuit=iqft_circ, do_swaps=True)

### Defining matrix operator
matrix = np.array([[1,0],[0,np.exp(1j*np.pi/4)]])

### Defining Quantum Phase Estimation object
eigs_qpe = EigsQPE(MatrixOperator(matrix=matrix),

### Generating circuit
eigs_circ = eigs_qpe.construct_circuit(mode="circuit")

2 - Then I want to prepend the following initial state to the previous circuit

vector = np.array([0,1])
initial_state = Custom(1,state_vector=vector)

Thus getting a QPE computed on the initial_state. Is it possible to do this, for example by accessing eigs_circ.data and prepending the proper gates that generate the initial_state?


1 Answer 1


If you have the circuit, you can get the registers the circuit acts on from eigs_circ.qregs. You can then create another circuit using the returned quantum register, add an instruction to it (initialize) and then add these two circuits together. Your final code should look something like

qregs = eigs_circ.qregs         # NB this is a list
qc = QuantumCircuit(*qregs)
qc.initialize([0,1], 0)         # You need to specify the qubits to act on
overall_circ = qc + eigs_circ

The method for appending two circuits is here

  • 1
    $\begingroup$ The line qc = QuantumCircuit(qregs) throws the error message AttributeError: 'list' object has no attribute 'name'. Nevertheless, qc = QuantumCircuit(*qregs) does the trick. $\endgroup$ Commented Feb 8, 2020 at 20:42
  • $\begingroup$ @NikPapadopoulos I'm glad this worked for you, I have updated the original answer with your correction - thanks! $\endgroup$
    – met927
    Commented Feb 10, 2020 at 9:49

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.