I have a state psi as an ndarray of shape (2 ** 3,) s.t.

psi[0]= amplitude of 000

psi[1] = amplitude of 001.

So my qubit ordering is reversed w.r.t. qiskit's. To initialize the circuit correctly and apply the IQFT on the first 2 qubits from the left I tried the following code:

import qiskit as qt
from qiskit.aqua.circuits import FourierTransformCircuits as QFT

    circuit = qt.QuantumCircuit(3)
    circuit.initialize( psi, [i for i in reversed(circuit.qubits)])

    QFT.construct_circuit(circuit=circuit, qubits=circuit.qubits[:2], inverse=True)

    backend = qt.Aer.get_backend('statevector_simulator')
    final_state = qt.execute(circuit, backend, shots=1).result().get_statevector()

From the tests I've run, final_state is not what I expected: defining

exact = np.kron(IQFT_matrix(2),np.eye(2)).dot(state) with IQFT_matrix(2)= IQFT_matrix for 2 qubits.

np.testing.assert_array_almost_equal(final_state, exact)

fails. Can you please help me find the problem?

  • $\begingroup$ Bits order is reversed on IBM Q. The most significant bit is on the right. $\endgroup$ – Martin Vesely Mar 13 '20 at 14:50
  • 1
    $\begingroup$ I know, that's why I initialized using reversed(circuit.qubits). Am I converting this the wrong way? I do not want to remap the state directly, since in future implementation the state will be many dimensional and I do not want to occupy tons of memory or perform a lot of operations on it. $\endgroup$ – simone Mar 13 '20 at 14:53

There is the parameter do_swaps when you construct the fourier transform circuit.

do_swaps (bool): Boolean flag to specify if swaps should be included to align
                 the qubit order of input and output. The output qubits would
                 be in reversed order without the swaps.
  • $\begingroup$ Thanks for your help. I've tried using this flag but it does not solve the problem: 'final_state' differs from what I expected not only for index mapping, but for values too. $\endgroup$ – simone Mar 13 '20 at 15:33

To change the endianness of a circuit in Qiskit you can use the reverse_bits method of the QuantumCircuit. To do so for the QFT you can pick the QFT from the circuit library, reverse it, and add it to your circuit:

from qiskit import QuantumCircuit
from qiskit.circuit.library import QFT

iqft = QFT(3, inverse=True)  # get the IQFT
reversed_bits_QFT = iqft.reverse_bits()  # reverse bit order

circuit = QuantumCircuit(3)
circuit.compose(reversed_bits_QFT, inplace=True)  # append your QFT 

Your Answer

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

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