|
| 1 | +# 在 Tensorcircuit 中使用参数化波形完成 Rabi 实验 |
| 2 | + |
| 3 | +## 下载安装 |
| 4 | +``` |
| 5 | +git clone https://github.com/ruan-prog/tensorcircuit.git |
| 6 | +cd tensorcircuit |
| 7 | +git checkout rabi_test |
| 8 | + |
| 9 | +cd experiments |
| 10 | +pip install -r requirements.txt |
| 11 | +``` |
| 12 | + |
| 13 | +## 代码实现 |
| 14 | + |
| 15 | +在 rabi.py 文件中修改代码 |
| 16 | +``` |
| 17 | +vim rabi.py |
| 18 | +``` |
| 19 | + |
| 20 | +#### 1. 配置量子云服务 |
| 21 | + |
| 22 | +```python |
| 23 | +set_token("your_api_token") # 需替换为真实的量子云服务令牌 |
| 24 | +``` |
| 25 | + |
| 26 | +#### 2. 自定义参数化波形 |
| 27 | + |
| 28 | + |
| 29 | +```python |
| 30 | +def gen_parametric_waveform_circuit(t): |
| 31 | + """ |
| 32 | + 参数: |
| 33 | + t : 脉冲持续时间(dt) |
| 34 | + |
| 35 | + 返回: |
| 36 | + Circuit: 包含自定义波形的量子电路 |
| 37 | + """ |
| 38 | + qc = Circuit(1) |
| 39 | + |
| 40 | + param0 = Param("a") |
| 41 | + |
| 42 | + builder = qc.calibrate("rabi_test", [param0]) |
| 43 | + builder.new_frame("drive_frame", param0) |
| 44 | + builder.play("drive_frame", waveforms.CosineDrag(t, 0.2, 0.0, 0.0)) |
| 45 | + |
| 46 | + builder.build() |
| 47 | + qc.add_calibration('rabi_test', ['q[0]']) |
| 48 | + |
| 49 | + tqasm_code = qc.to_tqasm() |
| 50 | + |
| 51 | + print(tqasm_code) |
| 52 | + return qc |
| 53 | +``` |
| 54 | + |
| 55 | + |
| 56 | +#### 3. 在量子设备中执行电路 |
| 57 | + |
| 58 | +通过以下输出获取可用的设备名称 |
| 59 | +```python |
| 60 | +ds = list_devices() |
| 61 | +``` |
| 62 | + |
| 63 | +在其中一台设备上执行电路 |
| 64 | +```python |
| 65 | +def run_circuit(qc): |
| 66 | + """ |
| 67 | + 参数: |
| 68 | + qc (Circuit): 待执行量子电路 |
| 69 | + |
| 70 | + 返回: |
| 71 | + rf(Dict): 测量结果统计 |
| 72 | + """ |
| 73 | + device_name = "tianji_m2" |
| 74 | + d = get_device(device_name) |
| 75 | + t = submit_task( |
| 76 | + circuit=qc, |
| 77 | + shots=shots_const, |
| 78 | + device=d, |
| 79 | + enable_qos_gate_decomposition=False, |
| 80 | + enable_qos_qubit_mapping=False, |
| 81 | + ) |
| 82 | + rf = t.results() |
| 83 | + return rf |
| 84 | +``` |
| 85 | + |
| 86 | +#### 4. Rabi 参数扫描 |
| 87 | + |
| 88 | +定义扫描周期,遍历不同的脉冲长度 |
| 89 | +```python |
| 90 | +def exp_rabi(): |
| 91 | + result_lst = [] |
| 92 | + for t in range(1, 400, 2): |
| 93 | + qc = gen_parametric_waveform_circuit(t) |
| 94 | + result = run_circuit(qc) |
| 95 | + result['duration'] = t |
| 96 | + result_lst.append(result) |
| 97 | + return result_lst |
| 98 | +``` |
| 99 | + |
| 100 | +#### 5. 绘制 Rabi 结果图 |
| 101 | + |
| 102 | +绘制 0/1 态的结果分布 |
| 103 | +```python |
| 104 | +def draw_rabi(result_lst): |
| 105 | + data = { |
| 106 | + 'duration': [], |
| 107 | + '0': [], |
| 108 | + '1': [] |
| 109 | + } |
| 110 | + |
| 111 | + for result in result_lst: |
| 112 | + data['0'].append(int(result['0']) / shots_const) |
| 113 | + data['1'].append(int(result['1']) / shots_const) |
| 114 | + data['duration'].append(result['duration']) |
| 115 | + |
| 116 | + plt.figure(figsize=(10,6)) |
| 117 | + plt.plot(data['duration'], data['0'], 'b-o', label='State |0>') |
| 118 | + plt.plot(data['duration'], data['1'], 'r--s', label='State |1>') |
| 119 | + |
| 120 | + plt.title('Rabi Oscillation Experiment') |
| 121 | + plt.xlabel('Duration (dt)') |
| 122 | + plt.ylabel('Probability') |
| 123 | + plt.grid(alpha=0.3) |
| 124 | + plt.legend() |
| 125 | + plt.tight_layout() |
| 126 | + |
| 127 | + plt.savefig('rabi.png', dpi=300) |
| 128 | + plt.show() |
| 129 | +``` |
0 commit comments