ข้ามไปยังเนื้อหาหลัก

พล็อตสถานะควอนตัม

Package versions

The code on this page was developed using the following requirements. We recommend using these versions or newer.

qiskit[all]~=2.3.0

ในหลายสถานการณ์ เช่น การเรียนรู้หรือการดีบัก การแสดงภาพสถานะของคอมพิวเตอร์ควอนตัมนั้นมีประโยชน์มาก ที่นี่เราสมมติว่าคุณมีสถานะเฉพาะเจาะจงแล้วจากการจำลองหรือ state tomography สามารถดูสถานะของระบบควอนตัมขนาดเล็กได้เท่านั้น

การใช้ผลลัพธ์จากฟังก์ชัน

ฟังก์ชันทั้งหมดในหน้านี้คืนออบเจกต์ที่มีรูปแบบสวยงาม เมื่อบรรทัดสุดท้ายของ code cell แสดงออบเจกต์เหล่านี้ Jupyter notebooks จะแสดงไว้ใต้ cell นั้น ถ้าเรียกใช้ฟังก์ชันเหล่านี้ในสภาพแวดล้อมอื่นหรือใน script จะต้อง show หรือ save ผลลัพธ์อย่างชัดเจน

ฟังก์ชันส่วนใหญ่คืนภาพซึ่งเป็น matplotlib.Figure objects มีสองตัวเลือก:

  • เรียก .show() บนออบเจกต์ที่คืนมาเพื่อเปิดภาพในหน้าต่างใหม่ (สมมติว่า matplotlib backend ที่กำหนดค่าไว้เป็นแบบ interactive)
  • เรียก .savefig("out.png") เพื่อบันทึก figure ไปที่ out.png ใน working directory ปัจจุบัน เมธอด savefig() รับ path ดังนั้นสามารถปรับตำแหน่งและชื่อไฟล์ที่บันทึกผลลัพธ์ได้ เช่น plot_state_city(psi).savefig("out.png")

ผลลัพธ์ LaTeX เป็น IPython.display.Latex objects ตัวเลือกที่ดีที่สุดในสภาพแวดล้อมที่ไม่ใช่ Jupyter คือหลีกเลี่ยงผลลัพธ์นี้โดยการพิมพ์สถานะเพื่อแสดงเป็นข้อความ หรือเปลี่ยนไปใช้ latex_source drawer เพื่อคืน LaTeX source string

สถานะควอนตัมคือ density matrix ρ\rho (Hermitian matrix) หรือ statevector ψ|\psi\rangle (complex vector) ความสัมพันธ์ระหว่าง density matrix กับ statevector คือ

ρ=ψψ,\rho = |\psi\rangle\langle \psi|,

และมีความทั่วไปกว่า เนื่องจากสามารถแทน mixed states ได้ (ผลรวมเชิงบวกของ statevectors)

ρ=kpkψkψk.\rho = \sum_k p_k |\psi_k\rangle\langle \psi_k |.

Qiskit แทนสถานะควอนตัมผ่านคลาส Statevector และ DensityMatrix และมีฟังก์ชันการแสดงภาพมากมาย ดูส่วนถัดจาก code cell ด้านล่างเพื่อดูว่าฟังก์ชันการแสดงภาพต่างๆ ของ Qiskit พล็อตสถานะควอนตัมต่อไปนี้อย่างไร

# Added by doQumentation — required packages for this notebook
!pip install -q qiskit
from math import pi
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector

# Create a Bell state for demonstration
qc = QuantumCircuit(2)
qc.h(0)
qc.crx(pi / 2, 0, 1)
psi = Statevector(qc)

แม้จะไม่ใช่ "พล็อต" ในทางเทคนิค แต่ Qiskit สามารถเรนเดอร์การแทนค่า LaTeX ของทั้ง Statevector และ DensityMatrix objects ที่แสดงได้สวยงามใน Jupyter notebooks ซึ่งเป็นไปตามแบบแผนทางคณิตศาสตร์มาตรฐานในการเขียนสถานะควอนตัม อ่านเพิ่มเติมใน Basics of quantum information: Single systems

Statevectors ใช้ "ket notation" เป็นค่าเริ่มต้น ส่วน density matrices แสดงเป็น matrix 2×2

นอกจากนี้ยังสามารถแทนที่ "latex" ด้วย "latex_source" เพื่อรับ raw LaTeX string

psi.draw("latex")  # psi is a Statevector object

2200+1201i211\frac{\sqrt{2}}{2} |00\rangle+\frac{1}{2} |01\rangle- \frac{i}{2} |11\rangle

from qiskit.quantum_info import DensityMatrix

DensityMatrix(psi).draw("latex") # convert to a DensityMatrix and draw
[122402i424140i400002i4i4014] \begin{bmatrix} \frac{1}{2} & \frac{\sqrt{2}}{4} & 0 & \frac{\sqrt{2} i}{4} \\ \frac{\sqrt{2}}{4} & \frac{1}{4} & 0 & \frac{i}{4} \\ 0 & 0 & 0 & 0 \\ - \frac{\sqrt{2} i}{4} & - \frac{i}{4} & 0 & \frac{1}{4} \\ \end{bmatrix}
from qiskit.visualization import plot_state_city

plot_state_city(psi)
# Alternative: psi.draw("city")

Output of the previous code cell

from qiskit.visualization import plot_state_hinton

plot_state_hinton(psi)
# Alternative: psi.draw("hinton")

Output of the previous code cell

from qiskit.visualization import plot_state_paulivec

plot_state_paulivec(psi)
# Alternative: psi.draw("paulivec")

Output of the previous code cell

from qiskit.quantum_info import SparsePauliOp

SparsePauliOp.from_operator(psi)
SparsePauliOp(['II', 'IX', 'XY', 'YI', 'YX', 'YZ', 'ZI', 'ZX', 'ZZ'],
coeffs=[ 0.25 +0.j, 0.1767767+0.j, -0.1767767+0.j, -0.125 +0.j,
-0.1767767+0.j, 0.125 +0.j, 0.125 +0.j, 0.1767767+0.j,
0.125 +0.j])
from qiskit.visualization import plot_state_qsphere

plot_state_qsphere(psi)
# Alternative: psi.draw("qsphere")

Output of the previous code cell

from qiskit.visualization import plot_bloch_multivector

plot_bloch_multivector(psi)
# Alternative: psi.draw("bloch")

Output of the previous code cell

ตัวเลือกสำหรับฟังก์ชันพล็อตสถานะ

ฟังก์ชันพล็อตสถานะทั้งหมดรับ arguments ต่อไปนี้ (ยกเว้น LaTeX drawer ที่ไม่คืน Matplotlib figure และ plot_state_qsphere ที่รับเฉพาะ figsize):

  • title (str): string สำหรับชื่อพล็อต แสดงที่ด้านบนของพล็อต
  • figsize (tuple): ขนาด figure เป็นนิ้ว (กว้าง, สูง)

ฟังก์ชัน plot_state_city และ plot_state_paulivec ยังรับ argument color (list ของ strings) ที่ระบุสีของแท่ง ดู API documentation สำหรับข้อมูลเพิ่มเติม

จำชื่อฟังก์ชันพล็อตที่ต้องการไม่ได้? ลองถาม Qiskit Code Assistant ดู

ขั้นตอนถัดไป

คำแนะนำ
  • ต้องการทบทวนความรู้ด้านข้อมูลควอนตัม? ดูหลักสูตร Basics of quantum information บน IBM Quantum Learning
  • อ่าน contributing guidelines ถ้าต้องการมีส่วนร่วมใน open-source Qiskit SDK
Source: IBM Quantum docs — updated 16 มี.ค. 2569
English version on doQumentation — updated 7 พ.ค. 2569
This translation based on the English version of 11 มี.ค. 2569