<jupyter_start><jupyter_text>Getting Start with IonQ and Cirq       View on QuantumAI        Run in Google Colab        View source on GitHub        Download notebook   This notebook shows how to get up and running with the IonQ API.  As of February 2021, this API is limited to partners only. More information about partnerships can be found at [ionq.com/get-started](https://ionq.com/get-started).To get started, first you must install Cirq. > NOTE: Currently this notebook requires the version of Cirq 0.10 or higher.<jupyter_code>try:
    import cirq
    version = cirq.__version__.split(".")
    assert int(version[0]) > 0 or int(version[1]) > 9, "Cirq version >0.9 required"
except (ImportError, AssertionError):
    print("Installing Cirq...")
    !pip install --quiet cirq
    print("Cirq installed.")<jupyter_output><empty_output><jupyter_text>Given that the above cell runs, you have installed Cirq and imported it.  To simplify using the ionq api it is also suggested that you import ionq. Notice how nice it is that cirq and ionq are four letter words ending in "q".<jupyter_code>import cirq
import cirq.ionq as ionq<jupyter_output><empty_output><jupyter_text>Constructing an IonQ Service object The main object that you use to access the IonQ API is an instance of the `cirq.ionq.Service` class.  To construct this you need two pieces of information: the location of the remote host for accessing the API and also an API key. Both should be supplied to partners. Please be careful when using notebooks and version control to not save your API key in a public location!Given these bits of information you get a service object by simply running<jupyter_code>REMOTE_HOST = 'https://example.com/'  # Replace with the IonQ host.
API_KEY = 'tomyheart' # Replace with your IonQ API key

service = ionq.Service(remote_host=REMOTE_HOST, 
                       api_key=API_KEY, 
                       default_target='simulator')<jupyter_output><empty_output><jupyter_text>Note that we have set the `default_target` to `simulator`.  The other option would be to set it to `qpu`. Running a simple circuitThe IonQ API supports a limited set of gates natively.  Circuit built with these gates do not need any modification and can be run directly against the API.  For a list of the API supported gates see [circuit documentation](../../ionq/circuit.md).  One supported gate is the square root of not gate, which we use here in conjunction with a controlled-not gate. The following cell will run the circuit below, blocking until the program has run and results have been returned:<jupyter_code>q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
    cirq.X(q0)**0.5,             # Square root of X
    cirq.CX(q0, q1),              # CNOT
    cirq.measure(q0, q1, key='b') # Measure both qubits
)
result = service.run(circuit, repetitions=100)
print(result)<jupyter_output>b=0100110001000110100000000110101111100110100001110110111010010011001011001100001001100110100011001111, 0100110001000110100000000110101111100110100001110110111010010011001011001100001001100110100011001111<jupyter_text>Because we did not specify a `target` and we ran this against a service with `default_target='simulator'`, this ran against the simulator. To run against the QPU simply add the target to the `run` method (note that this may take a while since the queue for the qpu is much longer than that for the simulator):<jupyter_code>result = service.run(circuit, repetitions=100, target='qpu')
print(result)<jupyter_output>b=1101101100001000011111111011110001111011100011101001000110000001001001011100111001000110000110111110, 1101101100001000011111111011110001111011100011101001000110000001001001011100111001000110000110111110<jupyter_text>JobsIn the above section, the `run` method blocked on awaiting the program to run and return results. A different pattern is to asynchronously create jobs, which return an id that they can be used to identify the job, and fetch the results at a later time.<jupyter_code>job = service.create_job(circuit, repetitions=100)
job_id = job.job_id()
print(f'Job id: {job_id}')<jupyter_output>Job id: 34be7d9b-1a27-4974-b8ca-2b5941e52cd8<jupyter_text>Given the `job` object above, you can check on the status of the job<jupyter_code>print(f'Status: {job.status()}')<jupyter_output>Status: completed<jupyter_text>Or if you only have the job id, you can use this to get the job and create a new `cirq.ionq.Job` object to query.<jupyter_code>same_job = service.get_job(job_id=job_id)
print(f'Status: {same_job.status()}')<jupyter_output>Status: completed<jupyter_text>To get the results from the job, you can get the results of the job using the `results()` method.  Note, however that this will block if the job is not completed, polling until the status is `completed`.<jupyter_code>results = same_job.results()
print(results)<jupyter_output>00: 0.5000000000000001
11: 0.4999999999999999<jupyter_text>Note that the results are not `cirq.Result`. To convert these to a `cirq.Result`, you can use `to_cirq_result()`<jupyter_code>print(results.to_cirq_result())<jupyter_output>b=0100001001111001110001111100101001010100010011100101011000111100110101100000110010110010001101111101, 0100001001111001110001111100101001010100010011100101011000111100110101100000110010110010001101111101
{'fraction_non_alphanumeric': 0.07154993392486313, 'fraction_numerical': 0.13026241268642627, 'mean_word_length': 5.344910179640719, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 38, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '100226', 'license': 'Apache-2.0', 'n_tokens_mistral': 2098, 'n_tokens_neox': 1579, 'n_words': 579, 'path': 'docs/tutorials/ionq/getting_started.ipynb', 'repo_name': 'albertonavaa/Cirq', 'size': '5297'}