Session for Variational Quantum Computing#
Limitations#
Currently only supports sampling
Currently requires specifying the session’s lifetime in advance (
VQSessionparameterlife_time)
A Simple Example#
Step 1> Define parameterized Ansatz#
For detailed information, please refer to Variational Quantum Circuit
# python code
from pyqpanda3.vqcircuit import VQCircuit
from pyqpanda3.core import *
vqc = VQCircuit()
vqc.set_Param([2])
vqc << RX(0, vqc.Param([0])) << RY(1, vqc.Param([1]))
Step 2> Define Ansatz gate parameters for iteration#
Initial parameters#
# python code
# Continue from the previous code on this page
import numpy as np
init_params = np.array([0.14, 1.13])
Parameter update strategy#
# python code
# Continue from the previous code on this page
def update_gate_params(old_params):
# Used to simulate parameter update, can be replaced with actual parameter update logic
return old_params * 2
Step 3> Enter variational computing session service#
1> Login#
# python code
# Continue from the previous code on this page
from qpanda3_runtime import RuntimeService
service.login('your_valid_api_key')
2> Apply for session#
Must pass a valid value for
life_timeto set the session’s survival time (unit: minutes)In the example code below, the real quantum computing chip with id WK_C180 is selected as the computing backend device
# python code
# Continue from the previous code on this page
with service.vqsession(
vqcircuit=vqc,
device=service.device('WK_C180'),
shots=10,
life_time=150
) as vqs:
#...code
Step 4> Add iteration logic in the session context#
# python code
# Continue from the previous code on this page
def check_for_stop_iter(params, count):
# Simulate checking whether to stop iteration
return count >= 4
params = init_params
count = 0
with service.vqsession(
vqcircuit=vqc,
device=service.device('WK_C180'),
shots=10,
life_time=150
) as vqs:
while True: # Simulate iteration process
vqtask = vqs.run_vqtask(params, measure_qubits)
res = vqtask.get_result_sync()
print(f'res:{res}')
count += 1
if check_for_stop_iter(params,count):
print('Iteration Completed')
break
else:
params = update_gate_params(params)
Complete Example Code#
# python code
from pyqpanda3.vqcircuit import VQCircuit
from pyqpanda3.core import *
import numpy as np
from qpanda3_runtime import RuntimeService
vqc = VQCircuit()
vqc.set_Param([2])
vqc << RX(0, vqc.Param([0])) << RY(1, vqc.Param([1]))
init_params = np.array([0.14, 1.13])
def update_gate_params(old_params):
# Used to simulate parameter update, can be replaced with actual parameter update logic
return old_params * 2
service = RuntimeService()
service.login('your_valid_api_key')
def check_for_stop_iter(params, count):
# Simulate checking whether to stop iteration
return count >= 4
measure_qubits = [0,1]
params = init_params
count = 0
with service.vqsession(
vqcircuit=vqc,
device=service.device('WK_C180'),
shots=10,
life_time=150
) as vqs:
while True: # Simulate iteration process
vqtask = vqs.run_vqtask(params, measure_qubits)
res = vqtask.get_result_sync()
print(f'res:{res}')
count += 1
if check_for_stop_iter(params,count):
print('Iteration Completed')
break
else:
params = update_gate_params(params)