I am trying to implement the one-qubit Approximate QFT for Shor's Algorithm in qiskit as described in this paper, which requires the gate $$ R_j^{\prime}=\left(\begin{array}{c} 1 \hspace{0.5em} 0 \\ 0 \hspace{0.5em} \phi_j^{\prime} \end{array}\right) \text { with } \phi_j^{\prime}=e^{-2 \pi i \sum_{k=2}^j m_{j-k} / 2^k} $$ in the circuit AQFT with one qubit

The variable $ m_{j-k} $ is a previous measurement result, so an array of previous results would be useful.
The alternative is to store each measurement in a separate classical bit and get the value of the register as a whole. As of now, the only way I know to access the classical register to implement gates is by using .c_if(c, val), which means that I will need to attach a c_if for every possible value of the classical register in order to construct the gate, creating way too many gates. Is there a way I can get the value of the classical register and store it somewhere?


1 Answer 1


I don't know if you considered something like this already, but you may try to look at the state of the qubit that was measured. Then you check: if the state is $|0\rangle$, you append the number "0" to an array "m"; conversely, if the state is $|1\rangle$, you append the number "1".

I haven't seen you code, but I've made a simple one myself in order to illustrate. Try to run it a few times.

from qiskit import BasicAer, transpile
from qiskit.quantum_info import Statevector

# Function that returns the state vector of a circuit

backend = BasicAer.get_backend("statevector_simulator")

def Simulate_statevector(q_circuit):
    tqc = transpile(q_circuit, backend)
    job = backend.run(tqc)
    result = job.result()
    psi_qc = result.get_statevector(tqc, 4)
    return Statevector(psi_qc)

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

qubits = QuantumRegister(2)
cbits = ClassicalRegister(2)

qc = QuantumCircuit(qubits, cbits)

qc.h(1) # Applying a H gate so that measurement in qubit 1 can return either |0> or |1>

m = [] # Array to store measurement outcomes

# Measuring qubit 1
qc.measure(1, 1)

# Getting the state vector of the circuit
Psi = Simulate_statevector(qc)

# Storing measurement outcome
if Psi[0].real == 1:

print('m =', m)

I hope it was useful.


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.