跳转至

Appendix F:How to conduct programming experiments on a cloud platform?

For advanced users of quantum cloud platforms, the process of submitting experiments through the website is time-consuming and laborious. For some more complex quantum circuits or those with other invocation requirements, simply using graphical or code writing in the graphical mode is not sufficient. Therefore, we offer an SDK calling mode, which allows cloud platform programming using Python.

The specific implementation method is as follows:

1.1 Prepare the login information for the quantum computing cloud platform.

(The "loginkey" below is just an example. Please go to your "Personal Center" to obtain it. https://quantumctek-cloud.com/user.html )

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 it, please make sure to update the above content with your own keys, otherwise the above keys will be periodically updated, and the example program will not work.

#User's key (SDK key), please check it in the User Center.

#Set the quantum computer code, which can be a physical machine, responder, or simulator.
#Currently available machine names:QC One (Superconducting quantum computer of similar scale to the 'Zuchongzhi-2')
#                Ansewering machines one (Responder, used for verifying experimental processes)
#                Simulator1(Simulator, usable for comparing experimental results or theoretical learning validation)
#Some machine types may require permissions to set or access, and the points consumption may vary with changes in activity policies. Please pay attention to your points balance in advance when running large-scale programs.
#The responder is specifically for program verification, providing immediate experiment completion upon submission. The circuit returns a fixed result, intended solely for verifying the program's logical functionality.

#Account Registration:https://quantumctek-cloud.com/login.html
#Account Permission Request: Contact service@quantumctek-cloud.com via email, with the subject 'Account Permission Request.' In the body of the email, please provide a clear description of your account name, the machine type, the number of bits, and bit numbering you wish to use.
#(Note: Do not provide your password; no staff member will ever ask for your password.)

1.2 Quantum Program Writing

In this text ,you will see examples of our quantum instruction set "QCIS". If you are using QASM, we also provide a QASM to QCIS conversion feature:

qcis=account.convert_qasm_to_qcis(qasm)
print(qcis)

Quantum programs can be written in various ways and can be run in multiple modes. As a basic tutorial, this document provides a sample circuit. For details, please refer to the full tutorial.

#Manually Writing Your First Quantum Program: Preparing Bell State
#It is strongly recommended to consider the impact of the bit topology structure when designing circuits, rather than performing bit mapping in the program.
qcis_circuit = '''
H Q0
X Q6
H Q6
CZ Q0 Q6
H Q6
M Q0
M Q6
'''

#qcis_circuit = [qcis_circuit]*10
#The above covers advanced features, which are explained in Chapter 4

print(qcis_circuit)
#Refer to the QCIS instruction set:https://docs.quantumctek-cloud.com/

#Starting from July 12, 2023, the initial bit numbering of the quantum chip has changed from 1 to 0. Therefore, for programs written by users before this date, it is necessary to subtract 1 from the bit numbering in order to continue running them, otherwise there may be issues with mismatched topologies.

1.3 Submitting the Program to the Quantum Computing Cloud Platform

As an introductory tutorial, you can submit an experiment using only the simplest submit_job() parameters. For more parameters, please refer to the advanced tutorial: https://docs.quantumctek-cloud.com/en/Advanced/4/

query_id=$\color{red} {submit\_job}$ (circuit=None, exp_name='exp0', parameters=None, values=None, num_shots=12000, lab_id=None, exp_id=None, version='version01', is_verify=True)**  
  • circuit, Quantum circuit, must be provided for new experiments.
  • exp_name, Experiment set name, in cases where lab_id is not provided, an experiment set will be created based on exp_name. However, the 'submit_job' function does not return the newly created experiment set ID. It is only recommended for rapid experiment execution. In cases where both lab_id and exp_name are provided, exp_name will be ignored.
  • parameters, values,Variable substitution in the circuit, used for hybrid programming, as explained in the subsequent tutorials.
  • num_shot, The number of times the experiment circuit is run. In quantum experiments, the results obtained are the statistical results of multiple experiments. The data range is from 1 to 10,000.
  • lab_id, Experiment set ID, equivalent to an experiment directory.
  • exp_id, Experiment version ID. If a circuit (circuit) is not provided, you can run a previously saved circuit by submitting exp_id.
  • version,Name of the experiment circuit.
  • query_id, Return value, character type. It represents the ID of the experiment run and is used to query the experiment results. If it is 0/empty, it indicates an exceptional experiment submission.

circuit, parameters, 'values' accepts a list type. For detailed usage, please refer to the advanced tutorial.
'submit_job' can be composed of multiple basic functions, for parameter definitions and more comprehensive usage, please see the advanced tutorial.

query_id = account.submit_job(circuit=qcis_circuit,num_shots=1000)
#For the simplest experiment submission, you only need to provide the experiment circuit
#But if you want to design an automated program, it's the best to provide all parameters clearly, and ensure that parameters are not duplicated within the same set, and generate them in advance using a format like time or counting.
#Since 'submit_job' has many parameters, it is recommended to use explicit parameter passing.
#'submit_job' can have more settings, so please stay tuned for our tutorial updates.
print(query_id)

1.4 Reading the Experimental 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. It is advisable to use a try function to run submit_job, so in case of unexpected situations, you can directly resubmit failed experiments to ensure the fully automated program's operation.

When query_id is not equal to 0, you can use query_experiment() for the next query task. If the query result returns 0, it is also an unexpected situation, and you can attempt a second query.

if query_id:
    result=account.query_experiment(query_id, max_wait_time=360000)
    #The expected format of the 'result' is as follows (for the latest updates, please refer to the FAQ section in the user manual)
    #The return value is a list, containing multiple dictionaries.
    #key:'results' represent the raw data of the circuit execution, totaling 1+num_shots data points. The first data point includes the bit numbers and their order in measurements, while the rest correspond to the results of each shot.
    #key:'probability' provides statistical data on the measurement results of the circuit, which has been adjusted in real-time.
    #When the number of measured bits exceeds 15, 'probability' will be empty. Users are advised to perform corrections based on the original data, taking into account the readout fidelity of the quantum computer at that time. Sample correction functions can be found in the advanced tutorial. Users can also develop their own correction functions.
    #The maximum waiting time is specified in seconds, with a default of 30 seconds if not provided. Quantum program execution may involve queuing, and the quantum computer itself has automatic calibration times. If you want to run fully automated programs, it is recommended to set the waiting time to be greater than both of these factors.

    #Below are the methods for displaying, utilizing, and saving experiment results.
    #Printing and Displaying Results
    print(result)
    #The experiment results are in the form of raw data, which can be lengthy. Here, we won't print them, but if you're interested in inspecting the structure of the experiment results, you can choose to print them.
    #The data includes the measurement results of each shot, making it suitable for flexible use. If you need to perform result statistics, refer to the advanced tutorial.
    #Selecting and Processing Example Results
    value = result
    #print(value)
    #Saving Results
    f = open("./results.txt",'w')
    f.write(str(value))
    f.close()
    print("The experiment results have been saved to disk.")
else :
    #If the experiment didn't run successfully, it requires subsequent resubmission or handling.
    print("In case of an abnormal experiment run, it needs to be resubmitted or rerun")

If you need to save the parameters of the quantum computation at this point for later reference, such as reading and making corrections, you can run the following command:

res=account.download_config()
#The complete machine parameters will be stored as a JSON file in the current directory.
print(res)

Additionally, the website also provides a programming environment where you can call the SDK using Python. To access it, you can either:

1. Go directly to the "Programming Lab," or

2. Create a "My Experiment Collection" and then create a "Programming Experiment."

The Programming Lab uses the Jupyter Hub environment, allowing you to use Python to encapsulate and call quantum computing instructions or compile code. If you need to conduct offline experiments, you'll need to install the runtime environment. However, since the Quantum Computing Cloud Platform has already pre-installed all the necessary environments, you can directly run your experiment code.