Skip to content
Permalink
Browse files

Add tests that run example python scripts (#2559)

* Add tests that run example python scripts

This commit adds a new test module that will execute all the example
scripts to verify they still work as we make changes. However, a number
of example scripts rely on using the ibmq service which is an optional
component now. To make filtering the example scripts between those which
don't need ibmq from those that need ibmq this also moves all the ibmq
examples into a self contained subdir in examples/python. Those examples
which require ibmq are run conditionally as online_tests. For example
scripts which were running on both local simulators and ibmq these are
split into 2 files one for just simulation and the other in ibmq that
uses the ibmq backends.

Fixes #970

* Fix whitespace in ghz

* Fix whitespace in qft

* Fix lint

* bit0 should not be a tuple

* Decode subprocess stdout and stderr

* Mark online tests as slow, since they run on devices

* Pivot to use subtest instead of ddt

* Update ibmq example based on rebase changes

* Add skips for windows/appveyor

The text drawer isn't working correctly on windows/appveyor. Until that
unrelated issue is fixed it will block this. So this commit adds a skip
on windows envs until issue #2616 is fixed.

* Remove outdated comment
  • Loading branch information...
mtreinish committed Jun 17, 2019
1 parent 9abef69 commit f5dd2e2753952ff7cdf340d3a917b0878a26e36b
@@ -17,8 +17,7 @@
"""

from qiskit import QuantumCircuit
from qiskit import IBMQ, BasicAer, execute
from qiskit.providers.ibmq import least_busy
from qiskit import BasicAer, execute


###############################################################
@@ -37,28 +36,8 @@
for i in range(num_qubits):
qc.measure(i, i)

###############################################################
# Set up the API and execute the program.
###############################################################
try:
IBMQ.load_accounts()
except:
print("""WARNING: No valid IBMQ credentials found on disk.
You must store your credentials using IBMQ.save_account(token, url).
For now, there's only access to local simulator backends...""")

# First version: simulator
sim_backend = BasicAer.get_backend('qasm_simulator')
job = execute(qc, sim_backend, shots=1024)
result = job.result()
print('Qasm simulator : ')
print(result.get_counts(qc))

# Second version: real device
least_busy_device = least_busy(IBMQ.backends(simulator=False,
filters=lambda x: x.configuration().n_qubits > 4))
print("Running on current least busy device: ", least_busy_device)
job = execute(qc, least_busy_device, shots=1024)
result = job.result()
print('Physical device (%s) : ' % least_busy_device)
print(result.get_counts(qc))
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""
GHZ state example. It also compares running on experiment and simulator.
"""

from qiskit import QuantumCircuit
from qiskit import IBMQ, execute
from qiskit.providers.ibmq import least_busy

###############################################################
# Make a quantum circuit for the GHZ state.
###############################################################
num_qubits = 5
qc = QuantumCircuit(num_qubits, num_qubits, name='ghz')

# Create a GHZ state
qc.h(0)
for i in range(num_qubits-1):
qc.cx(i, i+1)
# Insert a barrier before measurement
qc.barrier()
# Measure all of the qubits in the standard basis
for i in range(num_qubits):
qc.measure(i, i)

###############################################################
# Set up the API and execute the program.
###############################################################
IBMQ.load_accounts()

# Second version: real device
least_busy_device = least_busy(IBMQ.backends(simulator=False,
filters=lambda x: x.configuration().n_qubits > 4))
print("Running on current least busy device: ", least_busy_device)
job = execute(qc, least_busy_device, shots=1024)
result = job.result()
print('Physical device (%s) : ' % least_busy_device)
print(result.get_counts(qc))
File renamed without changes.
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""
Quantum Fourier Transform examples.
"""

import math
from qiskit import QuantumCircuit
from qiskit.providers.ibmq import least_busy, IBMQ

###############################################################
# make the qft
###############################################################
def input_state(circ, n):
"""n-qubit input state for QFT that produces output 1."""
for j in range(n):
circ.h(j)
circ.u1(-math.pi/float(2**(j)), j)


def qft(circ, n):
"""n-qubit QFT on q in circ."""
for j in range(n):
for k in range(j):
circ.cu1(math.pi/float(2**(j-k)), j, k)
circ.h(j)


qft3 = QuantumCircuit(5, 5, name="qft3")
qft4 = QuantumCircuit(5, 5, name="qft4")
qft5 = QuantumCircuit(5, 5, name="qft5")

input_state(qft3, 3)
qft3.barrier()
qft(qft3, 3)
qft3.barrier()
for j in range(3):
qft3.measure(j, j)

input_state(qft4, 4)
qft4.barrier()
qft(qft4, 4)
qft4.barrier()
for j in range(4):
qft4.measure(j, j)

input_state(qft5, 5)
qft5.barrier()
qft(qft5, 5)
qft5.barrier()
for j in range(5):
qft5.measure(j, j)

print(qft3)
print(qft4)
print(qft5)

###############################################################
# Set up the API and execute the program.
###############################################################
IBMQ.load_accounts()

# Second version: real device
least_busy_device = least_busy(IBMQ.backends(simulator=False,
filters=lambda x: x.configuration().n_qubits > 4))
print("Running on current least busy device: ", least_busy_device)
job = execute([qft3, qft4, qft5], least_busy_device, shots=1024)
result = job.result()
print(result.get_counts(qft3))
print(result.get_counts(qft4))
print(result.get_counts(qft5))
@@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""
Example showing how to use Qiskit-Terra at level 0 (novice).
This example shows the most basic way to user Terra. It builds some circuits
and runs them on both the BasicAer (local Qiskit provider) or IBMQ (remote IBMQ provider).
To control the compile parameters we have provided a transpile function which can be used
as a level 1 user.
"""

import time

# Import the Qiskit modules
from qiskit import QuantumCircuit, QiskitError
from qiskit import execute, IBMQ, BasicAer
from qiskit.providers.ibmq import least_busy
from qiskit.tools.monitor import job_monitor


IBMQ.load_accounts()


# making first circuit: bell state
qc1 = QuantumCircuit(2, 2)
qc1.h(0)
qc1.cx(0, 1)
qc1.measure([0,1], [0,1])

# making another circuit: superpositions
qc2 = QuantumCircuit(2, 2)
qc2.h([0,1])
qc2.measure([0,1], [0,1])

# setting up the backend
print("(BasicAER Backends)")
print(BasicAer.backends())

# running the job
job_sim = execute([qc1, qc2], BasicAer.get_backend('qasm_simulator'))
sim_result = job_sim.result()

# Show the results
print(sim_result.get_counts(qc1))
print(sim_result.get_counts(qc2))

# see a list of available remote backends
print("\n(IBMQ Backends)")
print(IBMQ.backends())

# Compile and run on a real device backend
try:
# select least busy available device and execute.
least_busy_device = least_busy(IBMQ.backends(simulator=False))
except:
print("All devices are currently unavailable.")

print("Running on current least busy device: ", least_busy_device)

# running the job
job_exp = execute([qc1, qc2], backend=least_busy_device, shots=1024, max_credits=10)

job_monitor(job_exp)
exp_result = job_exp.result()

# Show the results
print(exp_result.get_counts(qc1))
print(exp_result.get_counts(qc2))
@@ -39,12 +39,7 @@
from qiskit.providers.ibmq import least_busy
from qiskit.tools.monitor import job_monitor

try:
IBMQ.load_accounts()
except:
print("""WARNING: No valid IBMQ credentials found on disk.
You must store your credentials using IBMQ.save_account(token, url).
For now, there's only access to local simulator backends...""")
IBMQ.load_accounts()

# Making first circuit: bell state
qc1 = QuantumCircuit(2, 2, name="bell")
@@ -41,13 +41,7 @@
from qiskit.transpiler.passes import LookaheadSwap


try:
IBMQ.load_accounts()
except:
print("""WARNING: No valid IBMQ credentials found on disk.
You must store your credentials using IBMQ.save_account(token, url).
For now, there's only access to local simulator backends...""")

IBMQ.load_accounts()
# Making first circuit: superpositions
qc1 = QuantumCircuit(4, 4)
qc1.h(0)
@@ -19,7 +19,6 @@
import math
from qiskit import QuantumCircuit
from qiskit import execute, BasicAer
from qiskit.providers.ibmq import least_busy, IBMQ


###############################################################
@@ -67,30 +66,10 @@ def qft(circ, n):
print(qft4)
print(qft5)

###############################################################
# Set up the API and execute the program.
###############################################################
try:
IBMQ.load_accounts()
except:
print("""WARNING: No valid IBMQ credentials found on disk.
You must store your credentials using IBMQ.save_account(token, url).
For now, there's only access to local simulator backends...""")

print('Qasm simulator')
sim_backend = BasicAer.get_backend('qasm_simulator')
job = execute([qft3, qft4, qft5], sim_backend, shots=1024)
result = job.result()
print(result.get_counts(qft3))
print(result.get_counts(qft4))
print(result.get_counts(qft5))

# Second version: real device
least_busy_device = least_busy(IBMQ.backends(simulator=False,
filters=lambda x: x.configuration().n_qubits > 4))
print("Running on current least busy device: ", least_busy_device)
job = execute([qft3, qft4, qft5], least_busy_device, shots=1024)
result = job.result()
print(result.get_counts(qft3))
print(result.get_counts(qft4))
print(result.get_counts(qft5))
@@ -69,13 +69,15 @@
initial_layout = {q[0]: 0,
q[1]: 1,
q[2]: 2}
job = execute(qc, backend=backend, coupling_map=None, shots=1024, initial_layout=initial_layout)
job = execute(qc, backend=backend, coupling_map=None, shots=1024,
initial_layout=initial_layout)

result = job.result()
print(result.get_counts(qc))

# Second version: mapped to 2x8 array coupling graph
job = execute(qc, backend=backend, coupling_map=coupling_map, shots=1024,initial_layout=initial_layout)
job = execute(qc, backend=backend, coupling_map=coupling_map, shots=1024,
initial_layout=initial_layout)
result = job.result()
print(result.get_counts(qc))
# Both versions should give the same distribution
@@ -27,17 +27,7 @@

# Import the Qiskit modules
from qiskit import QuantumCircuit, QiskitError
from qiskit import execute, IBMQ, BasicAer
from qiskit.providers.ibmq import least_busy
from qiskit.tools.monitor import job_monitor


try:
IBMQ.load_accounts()
except:
print("""WARNING: No valid IBMQ credentials found on disk.
You must store your credentials using IBMQ.save_account(token, url).
For now, there's only access to local simulator backends...""")
from qiskit import execute, BasicAer

# making first circuit: bell state
qc1 = QuantumCircuit(2, 2)
@@ -61,26 +51,3 @@
# Show the results
print(sim_result.get_counts(qc1))
print(sim_result.get_counts(qc2))

# see a list of available remote backends
print("\n(IBMQ Backends)")
print(IBMQ.backends())

# Compile and run on a real device backend
try:
# select least busy available device and execute.
least_busy_device = least_busy(IBMQ.backends(simulator=False))
except:
print("All devices are currently unavailable.")

print("Running on current least busy device: ", least_busy_device)

# running the job
job_exp = execute([qc1, qc2], backend=least_busy_device, shots=1024, max_credits=10)

job_monitor(job_exp)
exp_result = job_exp.result()

# Show the results
print(exp_result.get_counts(qc1))
print(exp_result.get_counts(qc2))
@@ -413,7 +413,7 @@ def _map_condition(self, wire_map, condition):
# Map the register name, using fact that registers must not be
# fragmented by the wire_map (this must have been checked
# elsewhere)
bit0 = (condition[0], 0)
bit0 = condition[0][0]
new_condition = (wire_map.get(bit0, bit0).register, condition[1])
return new_condition

0 comments on commit f5dd2e2

Please sign in to comment.
You can’t perform that action at this time.