Appendix B: Complete Program Example¶
B.1 Prepare quantum computing cloud platform login information¶
from ezQgd import *
account = Account(login_key='opecT+SO+QFjLXREUU2f8paSJNtTytPPV8Dbbd2T8Zg=', machine_name='gd_qc1')
#For instance, set the user SDK key, and select a quantum computer.
#When using, please make sure to update the above key; otherwise, the key mentioned above will be periodically refreshed, and the example program will not function.
登录成功
B.2 Quantum program development¶
Still, try to find a way to implement the original program. The program's design is not covered in this tutorial. Please refer to other relevant tutorials.
#Manually writing the first quantum program: 3-qubit GHZ state.
#Using three arbitrarily chosen qubits, which may not conform to the topology and may require qubit mapping.
qcis_circuit = '''
H Q0
H Q7
CZ Q0 Q7
H Q7
H Q13
CZ Q7 Q13
H Q13
M Q0
M Q7
M Q13
'''
#The following two steps demonstrate the application of the OpenQASM program.
#1. Preparation of the QASM program; here, we provide the conversion from the previous example directly. The SDK offers a version of this conversion function for everyone to use.
qasm_circuit=account.convert_qcis_to_qasm(qcis_circuit)
print(qasm_circuit)
#2. Converting the QASM program into QCIS; the SDK offers a version of this conversion function for everyone to use.
#At this stage, the SDK has limited support for gate operations in this conversion. If you have specific requirements, you can contact us for additions or modify the conversion function according to your needs. Your contribution is greatly appreciated if you can share your work.
qcis_circuit2=account.convert_qasm_to_qcis(qasm_circuit)
print(qcis_circuit2)
#The gate functions directly used by the transformed function here differ from the original circuit, primarily due to some combinable operations in the transformation.
B.3 Program adjustment¶
Still, try to find a way to implement the primary program. The program's design is not covered in this tutorial. Please refer to other relevant tutorials.
B.3.1 Regular check¶
Check the program against QCIS syntax rules. Currently, an offline version is available, and topological structure check is not provided. An online version will be added in the future.
#Currently, the regular checks in this SDK are still based on the offline version provided within the SDK, and they do not include checks for topological structures, and so on.
#Subsequently, we will update to perform real-time regular checks using cloud platform services. This way, it ensures that circuit submissions after rule checks do not have any inherent issues.
#This regular function will transform the program to some extent, with the effect after regular checks for qcis_circuit2 being the same as qcis_circuit.
qcis_circuit2=account.qcis_check_regular(qcis_circuit2)
print(qcis_circuit2)
print('\n')
qcis_circuit3=account.qcis_check_regular(qcis_circuit)
print(qcis_circuit3)
B.3.2 Bit mapping¶
For circuits that were not designed with topological connections in mind, a topological mapping is required to make them runnable on the physical chip.
However, it's important to note that there are significant differences in mapping capabilities, and the implementation trade-offs of mapping algorithms vary. Please thoroughly understand the functionality and performance of the mapping function you choose.
Also, be aware that since mapping functions have some randomness during optimization, the results of each mapping may not be entirely the same each time.
Furthermore, if the original topology is too complex, it may lead to unsuccessful mapping in the end, or after using SWAP exchanges, the circuit depth significantly increases, affecting the execution results.
qcis_circuit = account.qcis_mapping_isq(qcis_circuit)
print(qcis_circuit)
print('\n')
qcis_circuit2 = account.qcis_mapping_isq(qcis_circuit2)
print(qcis_circuit2)
B.3.3 Circuit simplification¶
This is often the final step before submission, where potential optimizations in adjacent operations are merged. The optimizations provided by this SDK do not claim to be the best and are continuously evolving. We encourage everyone to actively provide feedback or participate in improvements.
We are accustomed to handing control over to the user, so the choice regarding the series of operations mentioned above is in the hands of the user. You can flexibly invoke or optimize certain functions based on your specific needs.
qcis_circuit = account.simplify(qcis_circuit)
print(qcis_circuit)
print('\n')
qcis_circuit2 = account.simplify(qcis_circuit2)
print(qcis_circuit2)
B.3.4 The underlying execution form of the circuit¶
This function can advance the circuit conversion into the basic gate functions supported by the physical machine, and this functionality is provided for users to understand the actual physical representation of the circuit. Whether or not this operation is executed, the transformation will be performed on the physical machine's end to ensure the quantum computer can run the circuit smoothly.
#The function is incomplete and not available.
B.4 Submit the program to the quantum computing cloud platform¶
Here, let's elaborate a bit on the experimental submission process.
B.4.1 Create an experiment collection¶
Creating an experiment collection is for better management of experiments to be changed in the future.
create_res = account.create_experiment('OpenAI')
if create_res == 0:
print('Failed to create a new experiment collection')
else :
print('Successfully created a new experiment collection,ID=', create_res)
lab_id = create_res
#The experiment collection ID is recommended to be kept for long-term use for future purposes.
B.4.2 Submit an experiment¶
Actually, the experiment submission function can be broken down, but for the sake of this example program, it is not necessary to delve into this process further. We won't elaborate on it any further.
Furthermore, the assignment function is explicitly explained in a specific tutorial, so we won't go into it in detail here.
Regarding how to handle unexpected experiment submission issues, please handle them as needed since the official system has not yet addressed all possible bugs.
query_id = account.submit_job(circuit=qcis_circuit, lab_id = lab_id, version="qcis_circuit22")
#For single experiment submissions, you can manually input the above parameters. If you wish to design an automated program, the 'version' parameter value can be generated in advance through methods like counting or other formats.
print(query_id)
B.5 Reading and processing results¶
B.5.1 Read experiment results¶
The previous steps have already submitted the prepared experiment to the quantum computer on the quantum computing cloud platform and executed it. You can simply retrieve the experiment results by using the query_id.
When using submit_job() to send the circuit to the physical quantum computer on the cloud platform, you will receive an experiment result query ID (query_id) for checking the experiment's progress. Please ensure to keep it safe.
If the returned query_id is 0, it means an error has occurred. The error details are usually directly output during the execution process.
So, it's a good idea to consider using a try function to run submit_job. In case of unexpected situations, you can simply resubmit failed experiments to ensure the smooth operation of the fully automated program.
When query_id is not 0, you can use query_experiment() to proceed with the next query task.
About how to query the results, and process the results, mainly depends on the user use scenarios, here is a complete example.
if query_id:
result=account.query_experiment(query_id, max_wait_time=360000)
#RESULT is the result of the experiment, a total of 1+num_shots arrays, the first data set is the bit number and order of the measurements, and the rest are the results corresponding to each shot.
#The maximum wait time is in seconds and defaults to 30 seconds when not passed. Since there will be queuing for the execution of the quantum program and the quantum computer itself has an auto-calibration time, if you want to run a fully automated program, the wait time should ideally be greater than both.
#The following shows the display, use and saving of the experiment results.
#Shows the probabilistic results for GHZ. This result is the probabilistic result with read correction performed directly when the quantum computer returns data.
#The system uses a fast read correction, resulting in a statistical probability of less than 0 for states with lower probabilities. Please use your discretion.
print(result[0].get('probability'))
#As a comparison, using the original data, a new probability statistic was performed.
probability_whole=account.readout_data_to_state_probabilities_whole(result[0])
print(probability_whole)
#Save the results, pay a little attention to the form in which the file is written, don't accidentally overwrite previously meaningful lines, and if necessary, design the filename as an automatically generated name.
f = open("./results.txt",'w')
f.write(str(result))
f.write('\n')
f.write(str(result[0].get('probability')))
f.write('\n')
f.write(str(probability_whole))
f.close()
print("The results of the experiment have been saved on disk。")
else :
#Experiment did not run successfully, requiring subsequent resubmission and other processing
print("Experiments run abnormally and need to be resubmitted or re-run")
If you need to save the parameters of the quantum computation at this point for later reference, you can run the following command:
Read and maintain quantum computer parameters for subsequent reference.
res=account.download_config()
#The complete parameters of the machine will be stored as a json file in the current directory.
print(res)
#These parameters may be used later in data processing, probability analysis and other scenarios. Please use your discretion.
#Currently the SDK (0.2.0.2) provides a read correction for reading the current quantum computer parameters, which will be changed to be compatible with user parameter input at a later date.
B.5.2 Processing and Use of Experimental Results¶
Currently for the measurement of bits less than 15 quantum bits, the quantum computer side of the server are the raw data will be counted, and ready to read correction returned.
If there are more than 15 measurement bits, the statistic result is returned as null, and the user needs to calculate the statistic result by himself and do the necessary read correction.
#probability statistics
probability_whole=account.readout_data_to_state_probabilities_whole(result[0])
print(probability_whole)
#The above function counts the entire space of the result
probability_part=account.readout_data_to_state_probabilities_part(result[0])
print(probability_part)
#The above function only counts the existing results, and the results with probability 0 will not appear. Favorable to save storage space. And the first type of complete statistics on memory consumption is extremely large.
#The read correction requires the experimentally obtained results and the quantum computer parameter file as input for the calculation.
#If no computer parameter file is provided, the current quantum computer parameters are used.
#When saving the results of an experiment, you can get them through the download_config() function.
probability_cali=account.probability_calibration(result[0], res)
print(probability_cali)
#This read correction is a correction for statistical probability, not by probabilistic resampling.
#If resampling is required to correct the data, users are also invited to design and share their own.