# How to select parts of qubits for visualization

This is an experiment. I drew a multi-qubit circuit, but I only need to visualize the qubits 0-3 in the end, that is, use the plot_state_city function, but it seems that something went wrong, and the prompt is as follows

The question seems to imply that the plot_state_city function is displaying all qubits, but I don't need that much

You can do that with snapshot_density_matrix Example:

from qiskit import QuantumCircuit
from qiskit.providers.aer import extensions  # import aer snapshot instructions
from qiskit import execute

qc = QuantumCircuit(3)
qc.h(0)
for i in range(qc.num_qubits-1):
qc.cx(i, i+1)
display(qc.draw())
qc.snapshot_density_matrix('density_matrix',[0,1])# [0,1]select the qubit you want to visualize
backend = Aer.get_backend('aer_simulator')
result = execute(qc, backend).result()
# Extract density matrix snapshot from result object:
density_matrix = result.data()['snapshots']['density_matrix']['density_matrix'][0]['value']
#print(density_matrix)
plot_state_city(density_matrix, color=['midnightblue', 'midnightblue'],
title="New State City")


but snapshot_density_matrix replace by SaveDensityMatrix in the future, so

from qiskit import QuantumCircuit, Aer,execute
from qiskit.visualization import plot_state_city
from qiskit.providers.aer.library import SaveDensityMatrix

qc = QuantumCircuit(3)
qc.h(0)
for i in range(qc.num_qubits-1):
qc.cx(i, i+1)
display(qc.draw())
qc.append(SaveDensityMatrix(2, label='density_matrix'),[0,1])
backend = Aer.get_backend('aer_simulator')
result = execute(qc, backend).result()
# Extract density matrix snapshot from result object:
density_matrix = result.data()['density_matrix']
#print(density_matrix)
plot_state_city(density_matrix, color=['midnightblue', 'midnightblue'],
title="New State City")
$$$$

• Thanks, this snapshot_density_matrix function has been deprecated, but thanks for the valuable ideas Jun 17 at 11:10
• @R-XZhao I didn't realize,then I will replace it with SaveDensityMatrix`, like the warn said, but it is the same thing.
– poig
Jun 17 at 13:59