<jupyter_start><jupyter_text>Example to use ProjectQ to run algorithms on Quantum Inspire Copyright 2018 QuTech Delft. Licensed under the Apache License, Version 2.0.For more information on Quantum Inspire, see https://www.quantum-inspire.com/.For more information on ProjectQ, see https://github.com/ProjectQ-Framework/ProjectQ.<jupyter_code>import logging
import os
from getpass import getpass

from projectq import MainEngine
from projectq.setups import linear
from projectq.ops import H, Rx, Rz, CNOT, CZ, Measure, All

from quantuminspire.credentials import load_account, get_token_authentication, get_basic_authentication
from quantuminspire.api import QuantumInspireAPI
from quantuminspire.projectq.backend_qx import QIBackend

QI_EMAIL = os.getenv('QI_EMAIL')
QI_PASSWORD = <PASSWORD>('QI_PASSWORD')
QI_URL = os.getenv('API_URL', 'https://api.quantum-inspire.com/')
token = load_account()
if token is not None:
    authentication = get_token_authentication(token)
else:
    if QI_EMAIL is None or QI_PASSWORD is None:
        print('Enter email')
        email = input()
        print('Enter password')
        password = <PASSWORD>()
    else:
        email, password = QI_EMAIL, QI_PASSWORD
    authentication = get_basic_authentication(email, password)

qi_api = QuantumInspireAPI(QI_URL, authentication)

projectq_backend = QIBackend(quantum_inspire_api=qi_api)<jupyter_output><empty_output><jupyter_text>Execute algorithm on QX simulator We create an algorithm to entangle qubit 0 and qubit 4.<jupyter_code>engine = MainEngine(backend=projectq_backend)  # create default compiler (simulator back-end)

qubits = engine.allocate_qureg(5)
q1 = qubits[0]
q2 = qubits[-1]

H | q1  # apply a Hadamard gate
CNOT | (q1, q2)
All(Measure) | qubits  # measure the qubits

engine.flush()  # flush all gates (and execute measurements)

print("Measured {}".format(','.join([str(int(q)) for q in qubits])))
print('Probabilities: %s' % (projectq_backend.get_probabilities(qubits),))
print(projectq_backend.cqasm())<jupyter_output>Measured 1,0,0,0,1
Probabilities: {'00000': 0.48828125, '10001': 0.51171875}
version 1.0
# cQASM generated by Quantum Inspire <class 'quantuminspire.projectq.backend_qx.QIBackend'> class
qubits 5

h q[0]
cnot q[0], q[4]<jupyter_text>The result is as expected: about half of the results is split between 0 and 1 on qubit 0 and 4. The QASM generated by the backend is fairly simple.  Simulate a spin-qubit array On a spin-qubit array we have limited connectivity and also a limited set of gates available. With ProjectQ we can handle these cases by adding specific compiler engines.Our engine lists is generated by the  `projectq.setups.linear` module.<jupyter_code>projectq_backend = QIBackend(quantum_inspire_api=qi_api)
engine_list = linear.get_engine_list(num_qubits=5, one_qubit_gates=(Rx, Rz), two_qubit_gates=(CZ,))
engine = MainEngine(backend=projectq_backend, engine_list=engine_list)  # create default compiler (simulator back-end)

qubits = engine.allocate_qureg(5)
q1 = qubits[0]
q2 = qubits[-1]

H | q1  # apply a Hadamard gate
CNOT | (q1, q2)
All(Measure) | qubits  # measure the qubits

engine.flush()  # flush all gates (and execute measurements)

print("Measured {}".format(','.join([str(int(q)) for q in qubits])))
print('Probabilities: %s' % (projectq_backend.get_probabilities(qubits),))
print(projectq_backend.cqasm())<jupyter_output>Your experiment can not be optimized and may take longer to execute, see https://www.quantum-inspire.com/kbase/optimization-of-simulations/ for details.<jupyter_text>The result is the same, but if we look at the QASM generated there is quite a difference. The CNOT gate was replaced by a CZ gate with some single qubit operations. Also the qubits 0 and 4 have been mapped to neighboring qubits.<jupyter_code>current_mapping = engine.mapper.current_mapping
for l, p in current_mapping.items():
    print('mapping logical qubit %d to physical qubit %d' % (l, p))<jupyter_output>mapping logical qubit 0 to physical qubit 0
mapping logical qubit 4 to physical qubit 1
mapping logical qubit 1 to physical qubit 2
mapping logical qubit 2 to physical qubit 3
mapping logical qubit 3 to physical qubit 4
{'fraction_non_alphanumeric': 0.09173652694610779, 'fraction_numerical': 0.019161676646706587, 'mean_word_length': 5.545454545454546, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '139973', 'license': 'Apache-2.0', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1299, 'n_words': 433, 'path': 'docs/example_projectq.ipynb', 'repo_name': 'NunoEdgarGFlowHub/quantuminspire', 'size': '4171'}