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

The ansatz

ดูวิดีโอที่ Victoria Lipinska อธิบายว่า ansatz คืออะไร และเหตุใดมันจึงสำคัญในบริบทของ variational quantum eigensolver (VQE)

เอกสารอ้างอิง

บทความต่อไปนี้ถูกอ้างอิงในวิดีโอข้างต้น

โค้ด Ansatz

ในบทเรียนก่อนหน้า คุณสร้าง Hamiltonian ที่อธิบายพลังงานของโมเลกุลที่สนใจ และแมปมันไปยังรูปแบบที่เหมาะสำหรับคอมพิวเตอร์ควอนตัม VQE ใช้ variational circuit เพื่อเตรียม quantum state จากนั้นเราใช้ state เหล่านั้นเพื่อหา expectation value ของ Hamiltonian (พลังงาน) พารามิเตอร์ใน variational circuit จะถูกปรับเปลี่ยนจนกว่าการคำนวณจะลู่เข้าสู่ expectation value ต่ำสุด ในบริบทของเคมีควอนตัม นี่ควรเป็นพลังงาน ground state บทเรียนนี้เน้นที่ variational circuit ซึ่งเรียกอีกชื่อว่า ansatz (คำภาษาเยอรมันที่แปลว่า "แนวทาง" หรือ "วิธีการ") ในบทเรียนนี้ คุณจะได้เรียนรู้

  • ชุด ansaetze สำเร็จรูปที่มีอยู่ใน circuit library
  • วิธีระบุหรือปรับเปลี่ยนคุณลักษณะของ ansatz
  • วิธีสร้าง ansatz ของตัวเอง
  • ตัวอย่างของ ansaetze ที่ดีและไม่ดี

Qiskit circuit library มี circuit หลายประเภทที่ใช้เป็น ansatz ได้ ที่นี่เราจะจำกัดการพูดถึงเฉพาะ two-local circuits (circuit ที่ประกอบด้วย Gate ที่ทำงานกับ Qubit ได้สูงสุดครั้งละสอง Qubit) Efficient SU2 เป็น ansatz ที่ใช้กันทั่วไป

circuit efficient_su_2 ประกอบด้วยชั้นของการดำเนินการ single qubit ที่ครอบคลุม SU(2) (special unity group อันดับ 2 เช่น Pauli rotation gates) และ CX entanglements รูปแบบนี้เป็น heuristic ที่มีประโยชน์ใน variational quantum algorithms อย่าง VQE และ classification circuits ใน quantum machine learning (QML)

เราจะเริ่มด้วยตัวอย่าง circuit efficient_su2 สี่ Qubit พร้อม SU(2) gates สองประเภท เช่น rx และ y รวมถึงระบุ entanglement scheme และจำนวนรอบ หากใช้ .draw() กับ circuit จะได้ภาพที่ค่อนข้างเป็นนามธรรม ภาพ circuit ที่เข้าใจง่ายกว่าจะได้จากการใช้ .decompose().draw() และที่นี่เราจะใช้ output = "mpl"

# Added by doQumentation — required packages for this notebook
!pip install -q qiskit
from qiskit.circuit.library import efficient_su2

SU2_ansatz = efficient_su2(4, su2_gates=["rx", "y"], entanglement="linear", reps=1)
print(SU2_ansatz.draw())
SU2_ansatz.decompose().draw(output="mpl")
┌──────────┐┌───┐     ┌──────────┐   ┌───┐
q_0: ┤ Rx(θ[0]) ├┤ Y ├──■──┤ Rx(θ[4]) ├───┤ Y ├─────────────────────
├──────────┤├───┤┌─┴─┐└──────────┘┌──┴───┴───┐ ┌───┐
q_1: ┤ Rx(θ[1]) ├┤ Y ├┤ X ├─────■──────┤ Rx(θ[5]) ├───┤ Y ├─────────
├──────────┤├───┤└───┘ ┌─┴─┐ └──────────┘┌──┴───┴───┐┌───┐
q_2: ┤ Rx(θ[2]) ├┤ Y ├────────┤ X ├─────────■──────┤ Rx(θ[6]) ├┤ Y ├
├──────────┤├───┤ └───┘ ┌─┴─┐ ├──────────┤├───┤
q_3: ┤ Rx(θ[3]) ├┤ Y ├────────────────────┤ X ├────┤ Rx(θ[7]) ├┤ Y ├
└──────────┘└───┘ └───┘ └──────────┘└───┘

Output of the previous code cell

SU(2) gates จะปรากฏที่จุดเริ่มต้นและจุดสิ้นสุด โดยมีลำดับและองค์ประกอบตามที่ระบุใน su2_gates = [...] รูปแบบ entanglement แบบ linear หมายความว่า CX gates จะไล่ผ่าน Qubit ที่มีหมายเลขเรียงกัน โดย entangle 0 กับ 1 จากนั้น 1 กับ 2 และต่อไปเรื่อย ๆ ตามแนวทแยงใน circuit ตามที่คาดไว้ การตั้ง reps = 2 จะเพิ่มชั้น entanglement และชั้น SU(2) สุดท้ายอีกชั้นหนึ่ง การตั้ง reps = n สอดคล้องกับชั้น entanglement n ชั้น โดยมีชั้น SU(2) คั่นกลางและอยู่ที่ปลายแต่ละด้าน

SU2_ansatz2 = efficient_su2(
4, su2_gates=["rx", "y", "z"], entanglement="linear", reps=2
)
SU2_ansatz2.decompose().draw(output="mpl")

Output of the previous code cell

มี entanglement scheme อื่น ๆ อีกหลายแบบ สองแบบที่ควรรู้คือ circular และ full Circular entanglement เหมือนกับ linear entanglement แต่มี CX gate เพิ่มเติมที่ entangle Qubit แรกกับ Qubit สุดท้าย รูปแบบ full entanglement มี CX gate ระหว่าง Qubit ทุกคู่ สังเกตว่าสำหรับ circuit ที่มี N Qubit จะมี N(N1)/2N(N-1)/2 CXCX gates ซึ่งอาจมีค่าการคำนวณสูง

SU2_ansatz3 = efficient_su2(
4, su2_gates=["rx", "y", "z"], entanglement="circular", reps=1
)
SU2_ansatz3.decompose().draw(output="mpl")

Output of the previous code cell

SU2_ansatz4 = efficient_su2(4, su2_gates=["rx", "y", "z"], entanglement="full", reps=1)
SU2_ansatz4.decompose().draw(output="mpl")

Output of the previous code cell

คุณสามารถตรวจสอบความลึกของ circuit ได้โดยใช้ .depth() หรือบางครั้ง .decompose().depth()

print(SU2_ansatz4.decompose().depth())
11

การนำไปขยายของ efficient_su2 คือ two-local circuit ซึ่งเป็นกรณีพิเศษของ n-local circuits two-local circuits ยังประกอบด้วยบล็อก SU(2) (หรือ rotation blocks) และ entanglement blocks ที่นี่เราสามารถระบุประเภทของ entanglement gates ที่ต้องการใช้ได้ เช่น CRX gates ในตัวอย่างนี้ทุก Gate รับพารามิเตอร์ แต่ไม่จำเป็นต้องเป็นเช่นนั้นเสมอไป อาจใช้ Y rotation gates และ CX entanglement gates ก็ได้

from qiskit.circuit.library import n_local

rotation_blocks = ["ry"]
entanglement_blocks = ["crx"]
two_ansatz = n_local(
4, rotation_blocks, entanglement_blocks, "linear", insert_barriers=True, reps=2
)
two_ansatz.decompose().draw(output="mpl")

Output of the previous code cell

ansatz สุดท้ายที่เราจะพูดถึงโดยชื่อคือ Pauli-two-design circuit นี้มีการหมุนเริ่มต้น RY(pi/4)RY(pi/4) และชั้น rotation ประกอบด้วย single qubit Pauli rotations ที่แกนถูกเลือกแบบสุ่มสม่ำเสมอให้เป็น X, Y หรือ Z ชั้น entanglement ประกอบด้วย pairwise CZ gates ที่มีความลึกรวมสอง สังเกตความแตกต่างใน entanglement (และความลึกของ circuit ทั้งหมด) ระหว่าง pauli_two_design นี้กับ efficient_su2 เป็นต้น

from qiskit.circuit.library import pauli_two_design

PtwoD_ansatz = pauli_two_design(5, reps=1, seed=10599, insert_barriers=True)
PtwoD_ansatz.decompose().draw(output="mpl")

Output of the previous code cell

variational circuits สำเร็จรูปเหล่านี้เป็น heuristics ที่มีประโยชน์ทั้งในแง่การบรรลุระดับ entanglement ที่ต้องการและการจำกัดความลึกของ circuit แต่ไม่มีอะไรพิเศษเกี่ยวกับมัน คุณสามารถสร้าง variational circuit ของตัวเองได้ และอาจมีข้อได้เปรียบในกรณีที่คุณรู้บางอย่างเกี่ยวกับ entanglement ของ target state ของระบบ

ในการสร้าง ansatz ของตัวเอง คุณเพียงแค่สร้าง quantum circuit โดยให้ Gate บางส่วนเป็นฟังก์ชันของสมาชิกใน parameter vector ("theta" ในตัวอย่างสาม Qubit ด้านล่าง)

from qiskit import QuantumCircuit
from qiskit.circuit import ParameterVector

n = 3

theta = ParameterVector("θ", length=n)
qc = QuantumCircuit(n)
qc.h(0)
qc.h(2)
for i in range(n - 1):
qc.cx(i, i + 1)
qc.cz(0, n - 1)
qc.barrier()
for i in range(n):
qc.ry(theta[i], i)
qc.barrier()
qc.cz(0, n - 1)
for i in reversed(range(n - 1)):
qc.cx(i, i + 1)
qc.h(0)
qc.h(1)
own_ansatz = qc
print(own_ansatz.depth())
qc.draw("mpl")
9

Output of the previous code cell

โดยทั่วไปแล้ว การเลือก ansatz ที่ดีที่สุดเป็นทั้งศาสตร์และศิลป์ ansatz ที่ดีที่สุดคือสิ่งที่ช่วยให้คุณไปถึงเป้าหมายด้วยขั้นตอนการ optimize น้อยที่สุด ง่ายกว่าที่จะระบุว่า ansaetze ใดมีแนวโน้มจะแย่ ตัวอย่างเช่น ความลึกของ circuit ที่มากขึ้นมักส่งผลให้เกิดการสะสมของข้อผิดพลาด การ mitigate ข้อผิดพลาดช่วยได้ แต่เป็นแนวปฏิบัติที่ดีที่จะรักษาความลึกของ circuit ให้ต่ำตามสมควร แต่อย่าตัด entanglement ที่จำเป็นออก คุณอาจมี target state ที่ต้องการ full entanglement scheme ด้านล่างมีสองตัวอย่างที่น่าจะเป็นตัวเลือกที่ไม่ดีด้วยเหตุผลที่ชัดเจน การเลือก ansatz ที่ดีจะกลับมาพูดถึงอีกครั้งในส่วนถัดไปในบริบทของการทดสอบการลู่เข้า

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

n = 4

theta = ParameterVector("θ", length=n)
qc = QuantumCircuit(n)
qc.h(0)
qc.h(2)
for i in range(n - 2):
qc.cx(i, i + 1)
qc.cz(0, n - 2)
qc.barrier()
for i in range(n):
qc.ry(theta[i], i)
qc.barrier()
qc.cz(0, n - 2)
for i in reversed(range(n - 2)):
qc.cx(i, i + 1)
qc.h(0)
qc.h(1)
own_ansatz2 = qc
print(own_ansatz2.depth())
qc.draw("mpl")
9

Output of the previous code cell

Circuit สุดท้ายนี้น่าจะเป็นตัวเลือกที่ไม่ดี เพราะความลึกของ Gate สูงมาก และการทำซ้ำชั้น entanglement สี่ครั้งไม่น่าจะให้ผลที่ตรงกับ target state ดีกว่าสองหรือสามครั้งอย่างมีนัยสำคัญ

su2_ansatz_long = efficient_su2(
4, su2_gates=["rx", "y", "z"], entanglement="linear", reps=4
)
print(su2_ansatz_long.decompose().depth())
su2_ansatz_long.decompose().draw(output="mpl")
24

Output of the previous code cell

Circuit เหล่านี้ยัง "ไม่สมบูรณ์" ในแง่ที่ยังมีพารามิเตอร์ที่ไม่ทราบค่าและเปลี่ยนแปลงได้อยู่ใน Gate หลายตัว พารามิเตอร์เหล่านั้นถูกเลือกโดยการเดาต่อเนื่อง และอัปเดตพารามิเตอร์เพื่อลด expectation value ของ cost function (ในบริบทเคมีคือพลังงาน ground state โดยทั่วไป) ในมิติหนึ่งหรือแม้แต่สองสามมิติ เรื่องนี้ไม่ใช่เรื่องยาก แต่ circuit ข้างต้นมีพารามิเตอร์ variational 20 ตัว ซึ่งหมายความว่าการหา target state ที่มีพลังงานต่ำสุดคือการค้นหาใน state 20 มิติ (อีกเหตุผลที่ไม่ควรใส่ Gate ที่ไม่จำเป็น) นี่คือจุดที่ classical optimizer เข้ามามีบทบาท และนั่นคือหัวข้อของบทเรียนถัดไป

Source: IBM Quantum docs — updated 9 ก.พ. 2569
English version on doQumentation — updated 7 พ.ค. 2569
This translation based on the English version of approx. 26 มี.ค. 2569