# How to get amplitudes only for chosen qubits?

For computation, I use N working qubits and M ancilla qubits.

qubits = QuantumRegister(N, name='q')
ancilla = QuantumRegister(M, name='anc')
circuit = QuantumCircuit(qubits, ancilla)


Then at the end of a program to get state vector I do:

result = execute(circuit, Aer.backend, shots=shots).result()
return result.get_statevector(circuit)


Because of ancilla qubits are usually used for computation/uncomputation steps it means that the end states of them are $$|0..0 \rangle$$ (at least in some cases) and I am not interested in them due to unnessasary information about M ancilla qubits contained in get_statevector(circuit).

Is it possible to get state vector so it will show amplitudes only for N working qubit?

I have an idea to solve this equation to find $$S_N$$ (state vector of N working qubits): $$S_N \otimes I_M = S_{M+N}$$ but probably qiskit can do it internally.

I think that the statevector_simulator will always return the statevector for all the qubits in the circuit. You can however add snapshots over a subset of qubits, so you could add this right at the end of your circuit and use the information you get from that instead.

from qiskit.extensions.simulator import snapshot
qc.snapshot("one_qubit", qubits=[0])
qc.snapshot("many_qubits", qubits=[0,2])

backend = Aer.get_backend('statevector_simulator')
result = execute(qc, backend).result()

snapshots = result.data()['snapshots']['statevector'].items()

• is qc a QuantumCircuit instance? – Kenenbek Arzymatov Sep 2 at 18:31
• I can't understand the output of result.data()['snapshots']['statevector'][snapshot_name]. Is it possible to get a state vector array with $2^N$ elements where $N$ is the number of working qubits. After the code snippet from your answer I get the following: – Kenenbek Arzymatov Sep 2 at 18:47
• [[[1.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]] – Kenenbek Arzymatov Sep 2 at 18:48
• It has entries for amplitudes for all the qubits, but the qubits which are not asked for are considered to be in the 0 state. This means you can do what you suggested below, and only take the number of amplitudes you need. For example qc = QuantumCircuit(2) qc.h(0) qc.h(1) qc.snapshot("one_qubit", qubits=[0]) qc.snapshot("many_qubits", qubits=[0,1])  returns many_qubits : [[[0.5000000000000001, 0.0], [0.5, 0.0], [0.5, 0.0], [0.4999999999999999, 0.0]]] one_qubit : [[[0.7071067811865476, 0.0], [0.7071067811865475, 0.0], [0.0, 0.0], [0.0, 0.0]]]  – met927 Sep 3 at 12:45
• (It may be easier to copy and paste that code and what it generates somewhere you can read it better!) – met927 Sep 3 at 12:47

If I can be sure that all ancilla qubits are in $$| 0...0 \rangle$$ then that their state vector is: $$\begin{bmatrix} 1 \\ 0 \\ ... \\ 0 \end{bmatrix}$$

with $$2^M\times 1$$ dimension.

So I can take the first $$2^N$$ elements out of result.get_statevector(circuit) to find the state vector of working qubits.

But it's just a partial solution.