เริ่มต้นใช้งาน Estimator
Estimator primitive คำนวณค่าความคาดหวัง (expectation values) สำหรับ observable หนึ่งรายการหรือมากกว่าที่สัมพันธ์กับ state ที่เตรียมโดย quantum circuits Circuit สามารถ parametrized ได้ ตราบที่ค่า parameter ถูกส่งมาพร้อมกับ primitive ด้วย
Primitive นี้มีเทคนิค error mitigation และ suppression ในตัวหลายอย่าง รวมถึง dynamical decoupling, Pauli-twirling, gate-folding ZNE, PEA และ PEC นอกจากนี้ยังรองรับตัวเลือก resilience_level ที่ช่วยให้กำหนดค่าการแลกเปลี่ยนระหว่างต้นทุนและความแม่นยำได้อย่างง่ายดาย
ขั้นตอนในหัวข้อนี้อธิบายวิธีตั้งค่า Estimator สำรวจตัวเลือกที่ใช้กำหนดค่า และเรียกใช้ในโปรแกรม
เวอร์ชันแพ็กเกจ
โค้ดในหน้านี้พัฒนาโดยใช้ requirements ต่อไปนี้ แนะนำให้ใช้เวอร์ชันเหล่านี้หรือใหม่กว่า
qiskit[all]~=2.4.0
qiskit-ibm-runtime~=0.46.1
# Added by doQumentation — required packages for this notebook
!pip install -q qiskit qiskit-ibm-runtime
```json
{/*Verified the v2 examples 2/29/24 - updated 10/29/24*/}
## ขั้นตอนการใช้ Estimator primitive \{#steps-to-use-the-estimator-primitive}
### 1. เริ่มต้นบัญชี \{#1-initialize-the-account}
เนื่องจาก Qiskit Runtime เป็นบริการที่จัดการให้ คุณต้องเริ่มต้นบัญชีก่อน จากนั้นจึงเลือก QPU ที่ต้องการใช้คำนวณค่าความคาดหวัง
ทำตามขั้นตอนใน [ตั้งค่าบัญชี IBM Cloud](cloud-setup) หากยังไม่มีบัญชีที่ตั้งค่าไว้
:::note[Fractional gates]
เพื่อใช้ [fractional gates](/guides/fractional-gates) ที่รองรับใหม่ ให้ตั้งค่า `use_fractional_gates=True` เมื่อขอ backend จาก `QiskitRuntimeService` instance ตัวอย่างเช่น:
```python
service = QiskitRuntimeService()
fractional_gate_backend = service.least_busy(use_fractional_gates=True)
นี่เป็นคุณสมบัติทดลองและอาจเปลี่ยนแปลงในอนาคต
:::
from qiskit_ibm_runtime import QiskitRuntimeService
service = QiskitRuntimeService()
backend = service.least_busy(
operational=True, simulator=False, min_num_qubits=127
)
print(backend.name)
ibm_fez
2. สร้าง circuit และ observable
คุณต้องมีอย่างน้อยหนึ่ง circuit และหนึ่ง observable เป็น input ให้ Estimator primitive
from qiskit.circuit.library import qaoa_ansatz
from qiskit.quantum_info import SparsePauliOp
entanglement = [tuple(edge) for edge in backend.coupling_map.get_edges()]
observable = SparsePauliOp.from_sparse_list(
[("ZZ", [i, j], 0.5) for i, j in entanglement],
num_qubits=backend.num_qubits,
)
circuit = qaoa_ansatz(observable, reps=2)
# The circuit is parametrized, so we will define the parameter values for execution
param_values = [0.1, 0.2, 0.3, 0.4]
Circuit และ observable ต้องถูกแปลงเพื่อใช้เฉพาะคำสั่งที่ QPU รองรับ (เรียกว่า instruction set architecture (ISA) circuits) ใช้ transpiler เพื่อทำสิ่งนี้
from qiskit.transpiler import generate_preset_pass_manager
pm = generate_preset_pass_manager(optimization_level=1, backend=backend)
isa_circuit = pm.run(circuit)
isa_observable = observable.apply_layout(isa_circuit.layout)
print(f">>> Circuit ops (ISA): {isa_circuit.count_ops()}")
>>> Circuit ops (ISA): OrderedDict([('rz', 4472), ('sx', 1884), ('cz', 1120)])
3. เริ่มต้น Qiskit Runtime Estimator
เมื่อเริ่มต้น Estimator ใช้พารามิเตอร์ mode เพื่อระบุโหมดที่ต้องการรัน ค่าที่เป็นไปได้คือ batch, session หรือ backend objects สำหรับ batch, session และ job execution mode ตามลำดับ สำหรับข้อมูลเพิ่มเติม ดู Introduction to Qiskit Runtime execution modes. โปรดทราบว่าผู้ใช้ Open Plan ไม่สามารถส่ง session jobs ได้
from qiskit_ibm_runtime import EstimatorV2 as Estimator
estimator = Estimator(mode=backend)
4. เรียกใช้ Estimator และรับผลลัพธ์
จากนั้น เรียกใช้เมธอด run() เพื่อคำนวณค่าความคาดหวังสำหรับ circuit และ observable ที่ป้อนเข้า Circuit, observable และชุดค่า parameter ทางเลือกถูกป้อนเป็น primitive unified bloc (PUB) tuples
job = estimator.run([(isa_circuit, isa_observable, param_values)])
print(f">>> Job ID: {job.job_id()}")
print(f">>> Job Status: {job.status()}")
>>> Job ID: d82869ntjchs73bnokog
>>> Job Status: QUEUED
result = job.result()
print(f">>> {result}")
print(f" > Expectation value: {result[0].data.evs}")
print(f" > Metadata: {result[0].metadata}")
>>> PrimitiveResult([PubResult(data=DataBin(evs=np.ndarray(<shape=(), dtype=float64>), stds=np.ndarray(<shape=(), dtype=float64>), ensemble_standard_error=np.ndarray(<shape=(), dtype=float64>)), metadata={'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32})], metadata={'dynamical_decoupling': {'enable': False, 'sequence_type': 'XX', 'extra_slack_distribution': 'middle', 'scheduling_method': 'alap'}, 'twirling': {'enable_gates': False, 'enable_measure': True, 'num_randomizations': 'auto', 'shots_per_randomization': 'auto', 'interleave_randomizations': True, 'strategy': 'active-accum'}, 'resilience': {'measure_mitigation': True, 'zne_mitigation': False, 'pec_mitigation': False}, 'version': 2})
> Expectation value: 30.60337496305257
> Metadata: {'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}
ขั้นตอนถัดไป
- เรียนรู้วิธี ทดสอบในเครื่อง ก่อนรันบนคอมพิวเตอร์ควอนตัม
- ทบทวน ตัวอย่าง โดยละเอียด
- ฝึกใช้ primitives โดยทำ Cost function lesson ใน IBM Quantum Learning
- เรียนรู้วิธี transpile ในเครื่องในส่วน Transpile
- ลองทำคู่มือ Compare transpiler settings
- เรียนรู้วิธี ใช้ primitive options
- ดู API สำหรับตัวเลือก Estimator
- อ่าน Migrate to V2 primitives