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

เริ่มต้นใช้งาน Approximate quantum compilation with tensor networks (AQC-Tensor)

เวอร์ชันของแพ็กเกจ

โค้ดในหน้านี้พัฒนาโดยใช้ requirements ต่อไปนี้ แนะนำให้ใช้เวอร์ชันเหล่านี้หรือใหม่กว่า

qiskit[all]~=2.3.0
qiskit-aer~=0.17
qiskit-addon-utils~=0.3.0
qiskit-addon-aqc-tensor[aer,quimb-jax]~=0.2.0; sys.platform != 'darwin'
scipy~=1.16.3

คู่มือนี้สาธิตตัวอย่างการใช้งาน AQC-Tensor แบบง่ายๆ เพื่อให้เริ่มต้นได้ง่าย ในตัวอย่างนี้ เราจะนำ Trotter Circuit ที่จำลองการวิวัฒนาการของ transverse field Ising model แล้วใช้วิธี AQC-Tensor เพื่อลด depth ของ Circuit ที่ได้ นอกจากนี้ตัวอย่างนี้ยังต้องใช้แพ็กเกจ qiskit-addon-utils สำหรับตัวสร้างปัญหา, qiskit-aer สำหรับการจำลอง tensor-network และ scipy สำหรับการปรับแต่ง parameter

เพื่อเริ่มต้น ให้นึกถึงว่า Hamiltonian ของ transverse field Ising model มีรูปแบบดังนี้

HIsing=i=1NJi,(i+1)ZiZi+1+hiXi\mathcal{H}_{Ising} = \sum_{i=1}^N J_{i,(i+1)}Z_iZ_{i+1} + h_i X_i

โดยเราจะสมมติให้มี periodic boundary conditions ซึ่งหมายความว่าสำหรับ i=10i=10 เราได้ i+1=111i+1=11\rightarrow 1 และ JJ คือความแข็งแรงของการจับคู่ระหว่างสองไซต์ และ hh คือความแข็งแรงของสนามแม่เหล็กภายนอก

โค้ดต่อไปนี้จะสร้าง Hamiltonian ของ Ising chain แบบ 10 ไซต์ที่มี periodic boundary conditions

# Added by doQumentation — required packages for this notebook
!pip install -q qiskit qiskit-addon-aqc-tensor qiskit-addon-utils qiskit-aer scipy
from qiskit.transpiler import CouplingMap
from qiskit_addon_utils.problem_generators import generate_xyz_hamiltonian
from qiskit.synthesis import SuzukiTrotter
from qiskit_addon_utils.problem_generators import (
generate_time_evolution_circuit,
)
from qiskit_addon_aqc_tensor import generate_ansatz_from_circuit
from qiskit_addon_aqc_tensor.simulation import tensornetwork_from_circuit
from qiskit_addon_aqc_tensor.simulation import compute_overlap
from qiskit_addon_aqc_tensor.objective import MaximizeStateFidelity
from qiskit_aer import AerSimulator
from scipy.optimize import OptimizeResult, minimize

# Generate some coupling map to use for this example
coupling_map = CouplingMap.from_heavy_hex(3, bidirectional=False)

# Choose a 10-qubit circle on this coupling map
reduced_coupling_map = coupling_map.reduce(
[0, 13, 1, 14, 10, 16, 4, 15, 3, 9]
)

# Get a qubit operator describing the Ising field model
hamiltonian = generate_xyz_hamiltonian(
reduced_coupling_map,
coupling_constants=(0.0, 0.0, 1.0),
ext_magnetic_field=(0.4, 0.0, 0.0),
)

แบ่งการวิวัฒนาการตามเวลาออกเป็นสองส่วนสำหรับการประมวลผลแบบคลาสสิกและแบบควอนตัม

เป้าหมายหลักของตัวอย่างนี้คือการจำลองการวิวัฒนาการตามเวลาของ model Hamiltonian เราทำสิ่งนี้โดยใช้ Trotter evolution ซึ่งจะแบ่งออกเป็นสองส่วน

  1. ส่วนแรกที่สามารถจำลองได้โดยใช้ matrix product states (MPS) ส่วนนี้จะเป็นสิ่งที่ถูก 'compiled' โดยใช้ AQC-Tensor
  2. ส่วนที่ตามมาซึ่งจะถูกประมวลผลบนฮาร์ดแวร์ควอนตัม

เราจะเลือกให้ระบบวิวัฒนาการถึงเวลา tf=5t_f=5 และใช้ AQC-Tensor เพื่อบีบอัดการวิวัฒนาการตามเวลาถึง t=4t=4 จากนั้นวิวัฒนาการโดยใช้ Trotter steps ปกติจนถึง tft_f

จากนี้เราจะสร้าง Circuit สองตัว ตัวแรกจะถูกบีบอัดโดยใช้ AQC-Tensor และตัวที่สองจะถูกประมวลผลบน QPU สำหรับ Circuit แรก เนื่องจากจะถูกจำลองแบบคลาสสิกโดยใช้ matrix product states เราจะใช้จำนวน layer ที่มากเพื่อลด Trotter error ในขณะที่ Circuit ที่สองจำลองการวิวัฒนาการตามเวลาจาก ti=4t_i=4 ถึง tf=5t_f=5 จะใช้จำนวน layer น้อยกว่ามากเพื่อลด depth

# Generate circuit to be compressed
aqc_evolution_time = 4.0
aqc_target_num_trotter_steps = 45

aqc_target_circuit = generate_time_evolution_circuit(
hamiltonian,
synthesis=SuzukiTrotter(reps=aqc_target_num_trotter_steps),
time=aqc_evolution_time,
)

# Generate circuit to execute on hardware
subsequent_evolution_time = 1.0
subsequent_num_trotter_steps = 5

subsequent_circuit = generate_time_evolution_circuit(
hamiltonian,
synthesis=SuzukiTrotter(reps=subsequent_num_trotter_steps),
time=subsequent_evolution_time,
)

เพื่อเปรียบเทียบ เราจะสร้าง Circuit ที่สามด้วย ซึ่งวิวัฒนาการถึง t=4t=4 แต่มีจำนวน layer เท่ากับ Circuit ที่สองที่วิวัฒนาการจาก ti=4t_i=4 ถึง tf=5t_f=5 นี่คือ Circuit ที่เรา จะใช้ หากเราไม่ได้ใช้เทคนิค AQC-Tensor เราจะเรียก Circuit นี้ว่า comparison circuit ไปก่อน

aqc_comparison_num_trotter_steps = int(
subsequent_num_trotter_steps
/ subsequent_evolution_time
* aqc_evolution_time
)
aqc_comparison_num_trotter_steps

comparison_circuit = generate_time_evolution_circuit(
hamiltonian,
synthesis=SuzukiTrotter(reps=aqc_comparison_num_trotter_steps),
time=aqc_evolution_time,
)

สร้าง ansatz และสร้างการจำลอง MPS

ต่อไปเราจะสร้าง ansatz ที่จะปรับแต่ง มันจะวิวัฒนาการในช่วงเวลาเดียวกันกับ Circuit แรกของเรา (จาก ti=0t_i=0 ถึง tf=4t_f=4) แต่ใช้ Trotter steps น้อยกว่า

เมื่อสร้าง Circuit แล้ว เราจะส่งต่อให้ฟังก์ชัน generate_ansatz_from_circuit() ของ AQC-Tensor ซึ่งวิเคราะห์การเชื่อมต่อแบบสอง-Qubit และคืนค่าสองสิ่ง สิ่งแรกคือ ansatz circuit ที่สร้างขึ้นซึ่งมีการเชื่อมต่อแบบสอง-Qubit เหมือนกัน และสิ่งที่สองคือชุด parameter ซึ่งเมื่อแทนลงใน ansatz จะได้ Circuit นำเข้า

aqc_ansatz_num_trotter_steps = 5

aqc_good_circuit = generate_time_evolution_circuit(
hamiltonian,
synthesis=SuzukiTrotter(reps=aqc_ansatz_num_trotter_steps),
time=aqc_evolution_time,
)

aqc_ansatz, aqc_initial_parameters = generate_ansatz_from_circuit(
aqc_good_circuit, qubits_initially_zero=True
)
aqc_ansatz.draw("mpl", fold=-1)

Output of the previous code cell

ต่อไปเราจะสร้างการแทนแทน MPS ของ state ที่จะถูกประมาณโดย AQC เราจะคำนวณ fidelity ψ1ψ22|\langle\psi_1 | \psi_2 \rangle |^2 ระหว่าง state ที่เตรียมโดย comparison circuit กับ Circuit ที่สร้าง target state (ซึ่งใช้ Trotter steps มากกว่า)

# Generate MPS simulator settings and obtain the MPS representation of the target state
simulator_settings = AerSimulator(
method="matrix_product_state",
matrix_product_state_max_bond_dimension=100,
)
aqc_target_mps = tensornetwork_from_circuit(
aqc_target_circuit, simulator_settings
)

# Compute the fidelity between the MPS representation of the target state and the state produced by the comparison circuit
comparison_mps = tensornetwork_from_circuit(
comparison_circuit, simulator_settings
)
comparison_fidelity = (
abs(compute_overlap(comparison_mps, aqc_target_mps)) ** 2
)
print(f"Comparison fidelity: {comparison_fidelity}")
Comparison fidelity: 0.9997111919739693

ปรับแต่ง parameter ของ ansatz โดยใช้ MPS

สุดท้าย เราจะปรับแต่ง ansatz circuit เพื่อให้ผลิต target state ได้ดีกว่า comparison_fidelity ของเรา ฟังก์ชันค่าใช้จ่ายที่จะลดให้น้อยที่สุดคือ MaximizeStateFidelity และจะถูกปรับแต่งโดยใช้ L-BFGS optimizer ของ scipy

objective = MaximizeStateFidelity(
aqc_target_mps, aqc_ansatz, simulator_settings
)

stopping_point = 1 - comparison_fidelity

def callback(intermediate_result: OptimizeResult):
print(f"Intermediate result: Fidelity {1 - intermediate_result.fun:.8}")
if intermediate_result.fun < stopping_point:
# Good enough for now
raise StopIteration

result = minimize(
objective,
aqc_initial_parameters,
method="L-BFGS-B",
jac=True,
options={"maxiter": 100},
callback=callback,
)
if (
result.status
not in (
0,
1,
99,
)
): # 0 => success; 1 => max iterations reached; 99 => early termination via StopIteration
raise RuntimeError(
f"Optimization failed: {result.message} (status={result.status})"
)

print(f"Done after {result.nit} iterations.")
aqc_final_parameters = result.x
Intermediate result: Fidelity 0.95084365
Intermediate result: Fidelity 0.98409893
Intermediate result: Fidelity 0.99142033
Intermediate result: Fidelity 0.99521405
Intermediate result: Fidelity 0.99566673
Intermediate result: Fidelity 0.99650054
Intermediate result: Fidelity 0.99683487
Intermediate result: Fidelity 0.99720426
Intermediate result: Fidelity 0.99761726
Intermediate result: Fidelity 0.99809073
Intermediate result: Fidelity 0.99838244
Intermediate result: Fidelity 0.99861841
Intermediate result: Fidelity 0.99874617
Intermediate result: Fidelity 0.99892696
Intermediate result: Fidelity 0.99908129
Intermediate result: Fidelity 0.99917737
Intermediate result: Fidelity 0.99925456
Intermediate result: Fidelity 0.99933134
Intermediate result: Fidelity 0.99947173
Intermediate result: Fidelity 0.99956469
Intermediate result: Fidelity 0.99964488
Intermediate result: Fidelity 0.99967419
Intermediate result: Fidelity 0.99968821
Intermediate result: Fidelity 0.9997448
Done after 24 iterations.

ณ จุดนี้เรามีชุด parameter ที่สร้าง target state ด้วย fidelity ที่สูงกว่าที่ comparison circuit จะสร้างได้โดยไม่ใช้ AQC ด้วย parameter ที่เหมาะสมเหล่านี้ compressed circuit ตอนนี้มี Trotter error น้อยลง และ depth น้อยกว่า Circuit เดิม

ขั้นตอนสุดท้าย โค้ดต่อไปนี้จะสร้าง full time evolution circuit ซึ่งสามารถส่งต่อไปยัง transpiler pipeline และประมวลผลบนฮาร์ดแวร์ควอนตัม

final_circuit = aqc_ansatz.assign_parameters(aqc_final_parameters)
final_circuit.compose(subsequent_circuit, inplace=True)
final_circuit.draw("mpl", fold=-1)

Output of the previous code cell

ขั้นตอนต่อไป

คำแนะนำ
Source: IBM Quantum docs — updated 13 ก.พ. 2569
English version on doQumentation — updated 7 พ.ค. 2569
This translation based on the English version of 11 มี.ค. 2569