Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up examples and README #357

Merged
merged 2 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 49 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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).

Expand All @@ -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.
Expand Down
13 changes: 4 additions & 9 deletions examples/python/basic_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions examples/python/ghz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

###############################################################
Expand Down
99 changes: 59 additions & 40 deletions examples/python/hello_quantum.py
Original file line number Diff line number Diff line change
@@ -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))
10 changes: 5 additions & 5 deletions examples/python/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
11 changes: 3 additions & 8 deletions examples/python/qft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions examples/python/rippleadd-async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading