diff --git a/README.md b/README.md index 05f915ead1e8..50cf523e428b 100644 --- a/README.md +++ b/README.md @@ -5,15 +5,15 @@ The Quantum Information Software Kit (**QISKit** for short) is a software development kit (SDK) for working with [OpenQASM](https://github.com/QISKit/qiskit-openqasm) and the -[IBM Q experience (QX)](https://quantumexperience.ng.bluemix.net/). +[IBM Q Experience (QX)](https://quantumexperience.ng.bluemix.net/). Use **QISKit** to create quantum computing programs, compile them, and execute them on one of several backends (online Real quantum processors, online simulators, and local simulators). For the online backends, QISKit uses our [python API client](https://github.com/QISKit/qiskit-api-py) -to connect to the IBM Q experience. +to connect to the IBM Q Experience. **We use GitHub issues for tracking requests and bugs. Please see the** -[IBM Q experience community](https://quantumexperience.ng.bluemix.net/qx/community) **for +[IBM Q Experience community](https://quantumexperience.ng.bluemix.net/qx/community) **for questions and discussion.** **If you'd like to contribute to QISKit, please take a look at our** @@ -64,7 +64,7 @@ If during the installation PIP doesn't succeed to build, don't worry, you will h #### Setup your environment -We recommend using python virtual environments to improve your experience. Refer to our +We recommend using python virtual environments to improve your Experience. Refer to our [Environment Setup documentation](doc/install.rst#3.1-Setup-the-environment) for more information. ## Creating your first Quantum Program @@ -76,40 +76,40 @@ We are ready to try out a quantum circuit example, which runs via the local simu This is a simple example that makes an entangled state. ```python -from qiskit import QuantumProgram, QISKitError, RegisterSizeError - -# Create a QuantumProgram object instance. -q_program = QuantumProgram() -backend = 'local_qasm_simulator' -try: - # Create a Quantum Register called "qr" with 2 qubits. - quantum_reg = q_program.create_quantum_register("qr", 2) - # Create a Classical Register called "cr" with 2 bits. - classical_reg = q_program.create_classical_register("cr", 2) - # Create a Quantum Circuit called "qc" involving the Quantum Register "qr" - # and the Classical Register "cr". - quantum_circuit = q_program.create_circuit("bell", [quantum_reg],[classical_reg]) - - # Add the H gate in the Qubit 0, putting this qubit in superposition. - quantum_circuit.h(quantum_reg[0]) - # Add the CX gate on control qubit 0 and target qubit 1, putting - # the qubits in a Bell state - quantum_circuit.cx(quantum_reg[0], quantum_reg[1]) - - # Add a Measure gate to see the state. - quantum_circuit.measure(quantum_reg, classical_reg) - - # Compile and execute the Quantum Program in the local_qasm_simulator. - result = q_program.execute(["bell"], backend=backend, shots=1024, seed=1) - - # Show the results. - print(result) - print(result.get_data("bell")) - -except QISKitError as ex: - print('There was an error in the circuit!. Error = {}'.format(ex)) -except RegisterSizeError as ex: - print('Error in the number of registers!. Error = {}'.format(ex)) +# Import the QISKit SDK +import qiskit + +# Create a Quantum Register called "qr" with 2 qubits +qr = qiskit.QuantumRegister("qr", 2) +# Create a Classical Register called "cr" with 2 bits +cr = qiskit.ClassicalRegister("cr", 2) +# Create a Quantum Circuit called involving "qr" and "cr" +qc = qiskit.QuantumCircuit(qr, cr) + +# Add a H gate on the 0th qubit in "qr", putting this qubit in superposition. +qc.h(qr[0]) +# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting +# the qubits in a Bell state. +qc.cx(qr[0], qr[1]) +# Add a Measure gate to see the state. +# (Ommiting the index applies an operation on all qubits of the register(s)) +qc.measure(qr, cr) + +# Create a Quantum Program for execution +qp = qiskit.QuantumProgram() +# Add the circuit you created to it, and call it the "bell" circuit. +# (You can add multiple circuits to the same program, for batch execution) +qp.add_circuit("bell", qc) + +# See a list of available local simulators +print("Local backends: ", qiskit.backends.discover_local_backends()) + +# Compile and run the Quantum Program on a simulator backend +sim_result = qp.execute("bell", backend='local_qasm_simulator', shots=1024, seed=1) + +# Show the results +print("simulation: ", sim_result) +print(sim_result.get_counts("bell")) ``` In this case, the output will be: @@ -119,21 +119,22 @@ COMPLETED {'counts': {'00': 512, '11': 512}} ``` -This script is available [here](examples/python/hello_quantum.py). +This script is available [here](examples/python/hello_quantum.py), where we also show how to +run the same program on a real quantum computer. ### Executing your code on a real Quantum chip You can also use QISKit to execute your code on a -[real Quantum Chip](https://github.com/QISKit/ibmqx-backend-information). -In order to do so, you need to configure the SDK for using the credentials for +[real quantum chip](https://github.com/QISKit/ibmqx-backend-information). +In order to do so, you need to configure the SDK for using the credentials in your Quantum Experience Account: -#### Configure your API token and QE credentials +#### Configure your API token and QX credentials -1. Create an _[IBM Q experience](https://quantumexperience.ng.bluemix.net) > Account_ if you haven't already done so. -2. Get an API token from the IBM Q experience website under _My Account > Personal Access Token_. This API token allows you to execute your programs with the IBM Q experience backends. See: [Example](doc/example_real_backend.rst). +1. Create an _[IBM Q Experience](https://quantumexperience.ng.bluemix.net) > Account_ if you haven't already done so. +2. Get an API token from the IBM Q Experience website under _My Account > Advanced > API Token_. This API token allows you to execute your programs with the IBM Q Experience backends. See: [Example](doc/example_real_backend.rst). 3. We are going to create a new file called `Qconfig.py` and insert the API token into it. This file must have these contents: ```python @@ -150,7 +151,7 @@ your Quantum Experience Account: 4. Substitute `MY_API_TOKEN` with your real API Token extracted in step 2. -5. If you have access to the IBM Q features, you also need to setup the +5. If you have access to the IBM Q network, you also need to setup the values for your hub, group, and project. You can do so by filling the `config` variable with the values you can find on your IBM Q account page. @@ -207,7 +208,7 @@ QISKit was originally developed by researchers and developers on the [IBM-Q](http://www.research.ibm.com/ibm-q/) Team at [IBM Research](http://www.research.ibm.com/), with the aim of offering a high level development kit to work with quantum computers. -Visit the [IBM Q experience community](https://quantumexperience.ng.bluemix.net/qx/community) for +Visit the [IBM Q Experience community](https://quantumexperience.ng.bluemix.net/qx/community) for questions and discussions on QISKit and quantum computing more broadly. If you'd like to contribute to QISKit, please take a look at our [contribution guidelines](CONTRIBUTING.rst). @@ -219,8 +220,8 @@ contribute to QISKit, please take a look at our [contribution guidelines](CONTRI ## Authors (alphabetical) Ismail Yunus Akhalwaya, Luciano Bello, Jim Challenger, Andrew Cross, Vincent Dwyer, Mark Everitt, Ismael Faro, -Jay Gambetta, Juan Gomez, Yunho Maeng, Paco Martin, Antonio Mezzacapo, Diego Moreda, Jesus Perez, -Russell Rundle, Todd Tilma, John Smolin, Erick Winston, Chris Wood. +Jay Gambetta, Juan Gomez, Ali Javadi-Abhari, Yunho Maeng, Paco Martin, Antonio Mezzacapo, Diego Moreda, +Jesus Perez, Russell Rundle, Todd Tilma, John Smolin, Erick Winston, Chris Wood. In future releases, anyone who contributes with code to this project is welcome to include their name here. diff --git a/examples/python/basic_optimize.py b/examples/python/basic_optimize.py index b53165071eb6..062bec5a2b38 100644 --- a/examples/python/basic_optimize.py +++ b/examples/python/basic_optimize.py @@ -16,16 +16,11 @@ # limitations under the License. # ============================================================================= -"""Demo basic optimization: Remove Zero Rotations and Remove Double CNOTs.""" +"""Demo basic optimization: Remove Zero Rotations and Remove Double CNOTs. -import os -import sys - -# We don't know from where the user is running the example, -# so we need a relative position from this file path. -# TODO: Relative imports for intra-package imports are highly discouraged. -# http://stackoverflow.com/a/7506006 -sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) +Note: if you have only cloned the QISKit repository but not +used `pip install`, the examples only work from the root directory. +""" from qiskit import QuantumProgram from qiskit import CompositeGate diff --git a/examples/python/ghz.py b/examples/python/ghz.py index 960d91bb3390..adb3f5d811e5 100644 --- a/examples/python/ghz.py +++ b/examples/python/ghz.py @@ -17,18 +17,12 @@ """ GHZ state example illustrating mapping onto the backend. -""" -import sys -import os +Note: if you have only cloned the QISKit repository but not +used `pip install`, the examples only work from the root directory. +""" -# We don't know from where the user is running the example, -# so we need a relative position from this file path. -# TODO: Relative imports for intra-package imports are highly discouraged. -# http://stackoverflow.com/a/7506006 -sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) from qiskit import QuantumProgram - import Qconfig ############################################################### diff --git a/examples/python/hello_quantum.py b/examples/python/hello_quantum.py index adfa1d4ddb86..658c58e4ec2c 100644 --- a/examples/python/hello_quantum.py +++ b/examples/python/hello_quantum.py @@ -1,59 +1,78 @@ """ -Example used in the readme. In this example a Bell state is made +Example used in the README. In this example a Bell state is made. + +Note: if you have only cloned the QISKit repository but not +used `pip install`, the examples only work from the root directory. """ -import sys -import os -from pprint import pprint -# so we need a relative position from this file path. -# TODO: Relative imports for intra-package imports are highly discouraged. -# http://stackoverflow.com/a/7506006 -sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) -from qiskit import QuantumProgram, QISKitError, RegisterSizeError - -# Create a QuantumProgram object instance. -Q_program = QuantumProgram() + +# Import the QISKit SDK +import qiskit +# Import the IBMQ Experience API +from IBMQuantumExperience import IBMQuantumExperience + +# Authenticate for access to remote backends try: import Qconfig - Q_program.set_api(Qconfig.APItoken, Qconfig.config["url"], verify=False, - hub=Qconfig.config["hub"], - group=Qconfig.config["group"], - project=Qconfig.config["project"]) + api = IBMQuantumExperience(token=Qconfig.APItoken, + config={'url': Qconfig.config['url']}) + remote_backends = qiskit.backends.discover_remote_backends(api) except: - offline = True print("""WARNING: There's no connection with IBMQuantumExperience servers. - cannot test I/O intesive tasks, will only test CPU intensive tasks - running the jobs in the local simulator""") + Have you initialized a Qconfig.py file with your personal token? + For now, there's only access to local simulator backends...""") +local_backends = qiskit.backends.discover_local_backends() -print("The backends available for use are:") -pprint(Q_program.available_backends()) -print("\n") -backend = 'local_qasm_simulator' try: # Create a Quantum Register called "qr" with 2 qubits. - qr = Q_program.create_quantum_register("qr", 2) + qr = qiskit.QuantumRegister("qr", 2) # Create a Classical Register called "cr" with 2 bits. - cr = Q_program.create_classical_register("cr", 2) - # Create a Quantum Circuit called "qc". involving the Quantum Register "qr" - # and the Classical Register "cr". - qc = Q_program.create_circuit("bell", [qr], [cr]) + cr = qiskit.ClassicalRegister("cr", 2) + # Create a Quantum Circuit called involving "qr" and "cr" + qc = qiskit.QuantumCircuit(qr, cr) - # Add the H gate in the Qubit 0, putting this qubit in superposition. + # Add a H gate on qubit 0, putting this qubit in superposition. qc.h(qr[0]) - # Add the CX gate on control qubit 0 and target qubit 1, putting - # the qubits in a Bell state + # Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting + # the qubits in a Bell state. qc.cx(qr[0], qr[1]) - # Add a Measure gate to see the state. qc.measure(qr, cr) - # Compile and execute the Quantum Program in the local_qasm_simulator. - result = Q_program.execute(["bell"], backend=backend, shots=1024, seed=1) + # Create a Quantum Program for execution + qp = qiskit.QuantumProgram() + # Add the circuit you created to it, and call it the "bell" circuit. + # (You can add multiple circuits to the same program, for batch execution) + qp.add_circuit("bell", qc) + + # Compile and run the Quantum Program on a simulator backend + print("(Local Backends)") + for backend in local_backends: + print(backend) + sim_result = qp.execute("bell", backend='local_qasm_simulator', shots=1024, seed=1) + + # Show the results + print("simulation: ", sim_result) + print(sim_result.get_counts("bell")) + + # Compile and run the Quantum Program on a real device backend + if remote_backends: + # see a list of available remote backends + print("\n(Remote Backends)") + for backend in remote_backends: + backend_status = api.backend_status(backend) + print(backend, backend_status) + + # select least busy available device and execute + device_status = [api.backend_status(backend) + for backend in remote_backends if "simulator" not in backend] + best_device = min([x for x in device_status if x['available']==True], + key=lambda x:x['pending_jobs']) + print("Running on current least busy device: ", best_device['backend']) + exp_result = qp.execute("bell", backend=best_device['backend'], shots=1024, timeout=300) - # Show the results. - print(result) - print(result.get_data("bell")) + # Show the results + print("experiment: ", exp_result) + print(exp_result.get_counts("bell")) -except QISKitError as ex: +except qiskit.QISKitError as ex: print('There was an error in the circuit!. Error = {}'.format(ex)) -except RegisterSizeError as ex: - print('Error in the number of registers!. Error = {}'.format(ex)) \ No newline at end of file diff --git a/examples/python/initialize.py b/examples/python/initialize.py index e0cfe3dff826..496cb71eefa5 100644 --- a/examples/python/initialize.py +++ b/examples/python/initialize.py @@ -16,15 +16,15 @@ # ============================================================================= """ -Demo the use of the InitializeGate class to prepare arbitrary pure states. +Example use of the initialize gate to prepare arbitrary pure states. + +Note: if you have only cloned the QISKit repository but not +used `pip install`, the examples only work from the root directory. """ import math -import os -import sys - -from qiskit.tools.visualization import plot_circuit from qiskit import QuantumProgram +from qiskit.tools.visualization import plot_circuit import Qconfig diff --git a/examples/python/qft.py b/examples/python/qft.py index da5416b0c62c..d3da80468258 100644 --- a/examples/python/qft.py +++ b/examples/python/qft.py @@ -17,17 +17,12 @@ """ Quantum Fourier Transform examples. + +Note: if you have only cloned the QISKit repository but not +used `pip install`, the examples only work from the root directory. """ -import sys -import os import math - -# We don't know from where the user is running the example, -# so we need a relative position from this file path. -# TODO: Relative imports for intra-package imports are highly discouraged. -# http://stackoverflow.com/a/7506006 -sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) from qiskit import QuantumProgram import Qconfig diff --git a/examples/python/rippleadd-async.py b/examples/python/rippleadd-async.py index 757a214d0255..60e9d9aea985 100644 --- a/examples/python/rippleadd-async.py +++ b/examples/python/rippleadd-async.py @@ -16,19 +16,15 @@ # ============================================================================= """ -Ripple adder example based on Cuccaro et al, quant-ph/0410184. +Ripple adder example based on Cuccaro et al., quant-ph/0410184. + +Note: if you have only cloned the QISKit repository but not +used `pip install`, the examples only work from the root directory. """ -import sys -import os import time - -# We don't know from where the user is running the example, -# so we need a relative position from this file path. -# TODO: Relative imports for intra-package imports are highly discouraged. -# http://stackoverflow.com/a/7506006 -sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) -from qiskit import QuantumProgram, QuantumCircuit +from qiskit import QuantumProgram +from qiskit import QuantumCircuit import Qconfig diff --git a/examples/python/rippleadd.py b/examples/python/rippleadd.py index 1dfbbf7f32f2..b26806c97f6e 100644 --- a/examples/python/rippleadd.py +++ b/examples/python/rippleadd.py @@ -16,18 +16,14 @@ # ============================================================================= """ -Ripple adder example based on Cuccaro et al, quant-ph/0410184. -""" +Ripple adder example based on Cuccaro et al., quant-ph/0410184. -import sys -import os +Note: if you have only cloned the QISKit repository but not +used `pip install`, the examples only work from the root directory. +""" -# We don't know from where the user is running the example, -# so we need a relative position from this file path. -# TODO: Relative imports for intra-package imports are highly discouraged. -# http://stackoverflow.com/a/7506006 -sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) -from qiskit import QuantumProgram, QuantumCircuit +from qiskit import QuantumProgram +from qiskit import QuantumCircuit import Qconfig diff --git a/examples/python/sympy_backends.py b/examples/python/sympy_backends.py index 582d4a3227be..f5281d223543 100644 --- a/examples/python/sympy_backends.py +++ b/examples/python/sympy_backends.py @@ -16,12 +16,13 @@ # limitations under the License. # ============================================================================= """ -This python file shows how to use the symbolic backends: -local_sympy_qasm_simulator and local_sympy_unitary_simulator +Example use of the symbolic simulator backends, which keep precise forms of +amplitudes. -These backends offer local simulators which apply -sympy to perform the symbolic evaluation of the circuit. +Note: if you have only cloned the QISKit repository but not +used `pip install`, the examples only work from the root directory. """ + import os from qiskit import QuantumProgram diff --git a/examples/python/teleport.py b/examples/python/teleport.py index 3b19254df93e..661e81c1d91b 100644 --- a/examples/python/teleport.py +++ b/examples/python/teleport.py @@ -17,14 +17,11 @@ """ Quantum teleportation example based on an OpenQASM example. + +Note: if you have only cloned the QISKit repository but not +used `pip install`, the examples only work from the root directory. """ -import sys -import os -# We don't know from where the user is running the example, -# so we need a relative position from this file path. -# TODO: Relative imports for intra-package imports are highly discouraged. -# http://stackoverflow.com/a/7506006 -sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) + from qiskit import QuantumProgram import Qconfig