0
$\begingroup$

enter image description here

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

enter image description here

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

$\endgroup$

1 Answer 1

1
$\begingroup$

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")
```
$\endgroup$
2
  • $\begingroup$ Thanks, this snapshot_density_matrix function has been deprecated, but thanks for the valuable ideas $\endgroup$ Jun 17, 2022 at 11:10
  • $\begingroup$ @R-XZhao I didn't realize,then I will replace it with SaveDensityMatrix, like the warn said, but it is the same thing. $\endgroup$
    – poig
    Jun 17, 2022 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.