2

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?

glS
  • 27,670
  • 7
  • 39
  • 126
simone
  • 21
  • 2

2 Answers2

1

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.
Steve Wood
  • 1,698
  • 6
  • 7
1

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

Cryoris
  • 2,993
  • 8
  • 15