0
$\begingroup$

How can I get statevector and/or blochsphere representation of qubits of my choice. For example I have 3 qubits with different gates being applied on each qubit. the qiskit state_vector simulator gives bloch sphere representation of states for all qubits in the circuit and qasm simulator also plots histogram of probability for all qubits in the circuit. How can I get the single, for instance, second qubit's probability histogram and/or bloch sphere representation?

Secondly, can we use 15 qubits for simulation and plot probability histogram and/or bloch sphere or two qubits only, because qiskit is giving memory error since it takes the whole circuit for plotting. Thanks

$\endgroup$
1
$\begingroup$

For the statevector, I do not think there is an official way to print out only the statevector of a single or multiple qubits in a circuit. You can probably do it by parsing through the statevector that is returned for the whole circuit, and doing some math based on the statevector returned and the state the qubit(s) is in for each state in the statevector.

For the probability though, it is much easier to do this.

Plotting probability of specific qubits

If you only care about the probability of certain qubits, and will not need the probability for the others, you can set up the circuit to only measure the qubits you care about:

qc = QuantumCircuit(15, 2)
qc.x(0)

# This will only measure qubit 0 and qubit 5
qc.measure([0, 5], [0, 1])

state_sim = Aer.get_backend("qasm_simulator")
job = execute(qc, sim)
counts = job.result().get_counts(qc)

print(counts)
'''
Output:
{'01': 1024}
Right qubit is qubit 0, left qubit is qubit 5. 
You can then call plot_histogram on counts.
'''

If you want to measure the whole circuit but then want to view the probability of only a single qubit, you can parse through the counts dictionary to retrieve the data for only the qubit you care about:

qc = QuantumCircuit(3, 3)
qc.h(range(3))
qc.measure(range(3), range(3))

state_sim = Aer.get_backend("qasm_simulator")
job = execute(qc, sim)
counts = job.result().get_counts(qc)

print(counts)

'''
Output
{
'000': 127, '001': 127, '010': 133, '011': 108, 
'100': 115, '101': 146, '110': 132, '111': 136
}
'''

# New dictionary to store out results in.
new_counts = {'0': 0,
              '1': 0}

# The qubit we want the probability for
show_qubit = 1

# Loop through the counts dictionary, adding the value to the 
#  respective key in new_counts based on what state the show_qubit is in
for count in counts:
    count_idx = (len(count) - 1) - show_qubit
    new_counts[count[count_idx]] = new_counts[count[count_idx]] + counts[count]

print(new_counts)
plot_histogram(new_counts)

'''
Output:
{'0': 515, '1': 509}
~Histogram picture below~
'''

enter image description here

If you want to view the probability of multiple qubits in this way, it is possible, but you will need to edit the loop and new_dictionary a bit to allow for multiple qubits.

$\endgroup$

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.