From 173a6440607dfc5adc9e82bc7e0ce18649b082b2 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 25 Sep 2024 12:53:11 -0400 Subject: [PATCH] Remove examples/ directory (#13218) * Remove examples/ directory This commit removes the examples/ directory from Qiskit. At one time in the past these scripts and qasm files served as a useful example for how to get started with Qiskit. However the patterns shown in these examples aren't very relevant anymore and have been superseded by the official documentation and the numerous tutorials out there now. While these examples still work as we've been running tests to validate they're still functional on every commit, there is little value in keeping them around. This commit removes the examples directory and all the files contained within. The test_examples unittest module is also removed since there is nothing left to test anymore. * Remove other examples usage in Makefile and CONTRIBUTING.md --- CONTRIBUTING.md | 8 +- Makefile | 12 +- examples/python/circuit_draw.py | 33 ----- examples/python/commutation_relation.py | 37 ------ examples/python/ghz.py | 41 ------- examples/python/initialize.py | 70 ----------- examples/python/load_qasm.py | 30 ----- examples/python/qft.py | 76 ------------ examples/python/rippleadd.py | 113 ------------------ examples/python/stochastic_swap.py | 96 --------------- examples/python/teleport.py | 82 ------------- examples/python/using_qiskit_terra_level_0.py | 43 ------- examples/qasm/entangled_registers.qasm | 20 ---- examples/qasm/plaquette_check.qasm | 21 ---- examples/qasm/simple.qasm | 5 - test/python/test_examples.py | 59 --------- tox.ini | 18 ++- 17 files changed, 16 insertions(+), 748 deletions(-) delete mode 100644 examples/python/circuit_draw.py delete mode 100644 examples/python/commutation_relation.py delete mode 100644 examples/python/ghz.py delete mode 100644 examples/python/initialize.py delete mode 100644 examples/python/load_qasm.py delete mode 100644 examples/python/qft.py delete mode 100644 examples/python/rippleadd.py delete mode 100644 examples/python/stochastic_swap.py delete mode 100644 examples/python/teleport.py delete mode 100644 examples/python/using_qiskit_terra_level_0.py delete mode 100644 examples/qasm/entangled_registers.qasm delete mode 100644 examples/qasm/plaquette_check.qasm delete mode 100644 examples/qasm/simple.qasm delete mode 100644 test/python/test_examples.py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 53043ffaa289..e196a0fe7333 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -440,21 +440,21 @@ you can do this faster with the `-n`/`--no-discover` option. For example: to run a module: ``` -tox -epy310 -- -n test.python.test_examples +tox -epy310 -- -n test.python.compiler.test_transpiler ``` or to run the same module by path: ``` -tox -epy310 -- -n test/python/test_examples.py +tox -epy310 -- -n test/python/compiler/test_transpiler.py ``` to run a class: ``` -tox -epy310 -- -n test.python.test_examples.TestPythonExamples +tox -epy310 -- -n test.python.compiler.test_transpiler.TestTranspile ``` to run a method: ``` -tox -epy310 -- -n test.python.test_examples.TestPythonExamples.test_all_examples +tox -epy310 -- -n test.python.compiler.test_transpiler.TestTranspile.test_transpile_non_adjacent_layout ``` Alternatively there is a makefile provided to run tests, however this diff --git a/Makefile b/Makefile index bea8a880e274..b2f37105d416 100644 --- a/Makefile +++ b/Makefile @@ -28,8 +28,7 @@ env: # Ignoring generated ones with .py extension. lint: pylint -rn qiskit test tools - tools/verify_headers.py qiskit test tools examples - pylint -rn --disable='invalid-name, missing-module-docstring, redefined-outer-name' examples/python/*.py + tools/verify_headers.py qiskit test tools tools/find_optional_imports.py tools/find_stray_release_notes.py @@ -37,18 +36,17 @@ lint: lint-incr: -git fetch -q https://github.com/Qiskit/qiskit-terra.git :lint_incr_latest tools/pylint_incr.py -j4 -rn -sn --paths :/qiskit/*.py :/test/*.py :/tools/*.py - tools/pylint_incr.py -j4 -rn -sn --disable='invalid-name, missing-module-docstring, redefined-outer-name' --paths ':(glob,top)examples/python/*.py' - tools/verify_headers.py qiskit test tools examples + tools/verify_headers.py qiskit test tools tools/find_optional_imports.py ruff: - ruff qiskit test tools examples setup.py + ruff qiskit test tools setup.py style: - black --check qiskit test tools examples setup.py + black --check qiskit test tools setup.py black: - black qiskit test tools examples setup.py + black qiskit test tools setup.py # Use the -s (starting directory) flag for "unittest discover" is necessary, # otherwise the QuantumCircuit header will be modified during the discovery. diff --git a/examples/python/circuit_draw.py b/examples/python/circuit_draw.py deleted file mode 100644 index b8efbbef4910..000000000000 --- a/examples/python/circuit_draw.py +++ /dev/null @@ -1,33 +0,0 @@ -# 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 draw a quantum circuit using Qiskit. -""" - -from qiskit import QuantumCircuit - - -def build_bell_circuit(): - """Returns a circuit putting 2 qubits in the Bell state.""" - qc = QuantumCircuit(2, 2) - qc.h(0) - qc.cx(0, 1) - qc.measure([0, 1], [0, 1]) - return qc - - -# Create the circuit -bell_circuit = build_bell_circuit() - -# Use the internal .draw() to print the circuit -print(bell_circuit) diff --git a/examples/python/commutation_relation.py b/examples/python/commutation_relation.py deleted file mode 100644 index 6dd71981fa20..000000000000 --- a/examples/python/commutation_relation.py +++ /dev/null @@ -1,37 +0,0 @@ -# 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. - -from qiskit import QuantumCircuit - -from qiskit.transpiler import PassManager -from qiskit.transpiler.passes import CommutationAnalysis, CommutativeCancellation - -circuit = QuantumCircuit(5) -# Quantum Instantaneous Polynomial Time example -circuit.cx(0, 1) -circuit.cx(2, 1) -circuit.cx(4, 3) -circuit.cx(2, 3) -circuit.z(0) -circuit.z(4) -circuit.cx(0, 1) -circuit.cx(2, 1) -circuit.cx(4, 3) -circuit.cx(2, 3) -circuit.cx(3, 2) - -print(circuit) - -pm = PassManager() -pm.append([CommutationAnalysis(), CommutativeCancellation()]) -new_circuit = pm.run(circuit) -print(new_circuit) diff --git a/examples/python/ghz.py b/examples/python/ghz.py deleted file mode 100644 index 51bafa9c2f66..000000000000 --- a/examples/python/ghz.py +++ /dev/null @@ -1,41 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2017, 2023. -# -# 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, transpile -from qiskit.providers.basic_provider import BasicSimulator - - -############################################################### -# 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) - -sim_backend = BasicSimulator() -job = sim_backend.run(transpile(qc, sim_backend), shots=1024) -result = job.result() -print("Basic simulator : ") -print(result.get_counts(qc)) diff --git a/examples/python/initialize.py b/examples/python/initialize.py deleted file mode 100644 index 3e47922d3304..000000000000 --- a/examples/python/initialize.py +++ /dev/null @@ -1,70 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2017, 2023. -# -# 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 use of the initialize gate to prepare arbitrary pure states. -""" - -import math -from qiskit import QuantumCircuit, transpile -from qiskit.providers.basic_provider import BasicSimulator - - -############################################################### -# Make a quantum circuit for state initialization. -############################################################### -circuit = QuantumCircuit(4, 4, name="initializer_circ") - -desired_vector = [ - 1 / math.sqrt(4) * complex(0, 1), - 1 / math.sqrt(8) * complex(1, 0), - 0, - 0, - 0, - 0, - 0, - 0, - 1 / math.sqrt(8) * complex(1, 0), - 1 / math.sqrt(8) * complex(0, 1), - 0, - 0, - 0, - 0, - 1 / math.sqrt(4) * complex(1, 0), - 1 / math.sqrt(8) * complex(1, 0), -] - -circuit.initialize(desired_vector, [0, 1, 2, 3]) - -circuit.measure([0, 1, 2, 3], [0, 1, 2, 3]) - -print(circuit) - -############################################################### -# Execute on a simulator backend. -############################################################### -shots = 10000 - -# Desired vector -print("Desired probabilities: ") -print([format(abs(x * x), ".3f") for x in desired_vector]) - -# Initialize on local simulator -sim_backend = BasicSimulator() -job = sim_backend.run(transpile(circuit, sim_backend), shots=shots) -result = job.result() - -counts = result.get_counts(circuit) - -qubit_strings = [format(i, "04b") for i in range(2**4)] -print("Probabilities from simulator: ") -print([format(counts.get(s, 0) / shots, ".3f") for s in qubit_strings]) diff --git a/examples/python/load_qasm.py b/examples/python/load_qasm.py deleted file mode 100644 index af4878c41866..000000000000 --- a/examples/python/load_qasm.py +++ /dev/null @@ -1,30 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2017, 2023. -# -# 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 on how to load a file into a QuantumCircuit.""" - -from qiskit import QuantumCircuit -from qiskit.providers.basic_provider import BasicSimulator - -circ = QuantumCircuit.from_qasm_file("examples/qasm/entangled_registers.qasm") -print(circ) - -# See the backend -sim_backend = BasicSimulator() - -# Compile and run the Quantum circuit on a local simulator backend -job_sim = sim_backend.run(circ) -sim_result = job_sim.result() - -# Show the results -print("simulation: ", sim_result) -print(sim_result.get_counts(circ)) diff --git a/examples/python/qft.py b/examples/python/qft.py deleted file mode 100644 index ca22d0d60538..000000000000 --- a/examples/python/qft.py +++ /dev/null @@ -1,76 +0,0 @@ -# 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 import transpile -from qiskit.providers.basic_provider import BasicSimulator - - -############################################################### -# 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.p(-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.cp(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) - -print("Basic simulator") -sim_backend = BasicSimulator() -job = sim_backend.run(transpile([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)) diff --git a/examples/python/rippleadd.py b/examples/python/rippleadd.py deleted file mode 100644 index 6cb40f424c5b..000000000000 --- a/examples/python/rippleadd.py +++ /dev/null @@ -1,113 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2017, 2023. -# -# 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. - -""" -Ripple adder example based on Cuccaro et al., quant-ph/0410184. - -""" - -from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit -from qiskit import transpile -from qiskit.providers.basic_provider import BasicSimulator - -############################################################### -# Set the backend name and coupling map. -############################################################### -backend = BasicSimulator() -coupling_map = [ - [0, 1], - [0, 8], - [1, 2], - [1, 9], - [2, 3], - [2, 10], - [3, 4], - [3, 11], - [4, 5], - [4, 12], - [5, 6], - [5, 13], - [6, 7], - [6, 14], - [7, 15], - [8, 9], - [9, 10], - [10, 11], - [11, 12], - [12, 13], - [13, 14], - [14, 15], -] - -############################################################### -# Make a quantum program for the n-bit ripple adder. -############################################################### -n = 2 - -a = QuantumRegister(n, "a") -b = QuantumRegister(n, "b") -cin = QuantumRegister(1, "cin") -cout = QuantumRegister(1, "cout") -ans = ClassicalRegister(n + 1, "ans") -qc = QuantumCircuit(a, b, cin, cout, ans, name="rippleadd") - - -def majority(p, a, b, c): - """Majority gate.""" - p.cx(c, b) - p.cx(c, a) - p.ccx(a, b, c) - - -def unmajority(p, a, b, c): - """Unmajority gate.""" - p.ccx(a, b, c) - p.cx(c, a) - p.cx(a, b) - - -# Build a temporary subcircuit that adds a to b, -# storing the result in b -adder_subcircuit = QuantumCircuit(cin, a, b, cout) -majority(adder_subcircuit, cin[0], b[0], a[0]) -for j in range(n - 1): - majority(adder_subcircuit, a[j], b[j + 1], a[j + 1]) -adder_subcircuit.cx(a[n - 1], cout[0]) -for j in reversed(range(n - 1)): - unmajority(adder_subcircuit, a[j], b[j + 1], a[j + 1]) -unmajority(adder_subcircuit, cin[0], b[0], a[0]) - -# Set the inputs to the adder -qc.x(a[0]) # Set input a = 0...0001 -qc.x(b) # Set input b = 1...1111 -# Apply the adder -qc &= adder_subcircuit -# Measure the output register in the computational basis -for j in range(n): - qc.measure(b[j], ans[j]) -qc.measure(cout[0], ans[n]) - -############################################################### -# execute the program. -############################################################### - -# First version: not mapped -job = backend.run(transpile(qc, backend=backend, coupling_map=None), shots=1024) -result = job.result() -print(result.get_counts(qc)) - -# Second version: mapped to 2x8 array coupling graph -job = backend.run(transpile(qc, basis_gates=["u", "cx"], coupling_map=coupling_map), shots=1024) -result = job.result() -print(result.get_counts(qc)) - -# Both versions should give the same distribution diff --git a/examples/python/stochastic_swap.py b/examples/python/stochastic_swap.py deleted file mode 100644 index 7625cba3b73a..000000000000 --- a/examples/python/stochastic_swap.py +++ /dev/null @@ -1,96 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2017, 2019. -# -# 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 of using the StochasticSwap pass.""" - -from qiskit.transpiler.passes import StochasticSwap -from qiskit.transpiler import CouplingMap -from qiskit.converters import circuit_to_dag -from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit - -coupling = CouplingMap([[0, 1], [1, 2], [1, 3]]) -qr = QuantumRegister(4, "q") -cr = ClassicalRegister(4, "c") -circ = QuantumCircuit(qr, cr) -circ.cx(qr[1], qr[2]) -circ.cx(qr[0], qr[3]) -circ.measure(qr[0], cr[0]) -circ.h(qr) -circ.cx(qr[0], qr[1]) -circ.cx(qr[2], qr[3]) -circ.measure(qr[0], cr[0]) -circ.measure(qr[1], cr[1]) -circ.measure(qr[2], cr[2]) -circ.measure(qr[3], cr[3]) -dag = circuit_to_dag(circ) -# ┌─┐┌───┐ ┌─┐ -# q_0: |0>─────────────────■──────────────────┤M├┤ H ├──■─────┤M├ -# ┌───┐ │ └╥┘└───┘┌─┴─┐┌─┐└╥┘ -# q_1: |0>──■───────┤ H ├──┼───────────────────╫──────┤ X ├┤M├─╫─ -# ┌─┴─┐┌───┐└───┘ │ ┌─┐ ║ └───┘└╥┘ ║ -# q_2: |0>┤ X ├┤ H ├───────┼─────────■─────┤M├─╫────────────╫──╫─ -# └───┘└───┘ ┌─┴─┐┌───┐┌─┴─┐┌─┐└╥┘ ║ ║ ║ -# q_3: |0>───────────────┤ X ├┤ H ├┤ X ├┤M├─╫──╫────────────╫──╫─ -# └───┘└───┘└───┘└╥┘ ║ ║ ║ ║ -# c_0: 0 ═══════════════════════════════╬══╬══╩════════════╬══╩═ -# ║ ║ ║ -# c_1: 0 ═══════════════════════════════╬══╬═══════════════╩════ -# ║ ║ -# c_2: 0 ═══════════════════════════════╬══╩════════════════════ -# ║ -# c_3: 0 ═══════════════════════════════╩═══════════════════════ -# -# ┌─┐┌───┐ ┌─┐ -# q_0: |0>────────────────────■──┤M├┤ H ├──────────────────■──┤M├────── -# ┌─┴─┐└╥┘└───┘┌───┐┌───┐ ┌─┴─┐└╥┘┌─┐ -# q_1: |0>──■───X───────────┤ X ├─╫──────┤ H ├┤ X ├─X────┤ X ├─╫─┤M├─── -# ┌─┴─┐ │ ┌───┐└───┘ ║ └───┘└─┬─┘ │ └───┘ ║ └╥┘┌─┐ -# q_2: |0>┤ X ├─┼──────┤ H ├──────╫─────────────■───┼──────────╫──╫─┤M├ -# └───┘ │ ┌───┐└───┘ ║ │ ┌─┐ ║ ║ └╥┘ -# q_3: |0>──────X─┤ H ├───────────╫─────────────────X─┤M├──────╫──╫──╫─ -# └───┘ ║ └╥┘ ║ ║ ║ -# c_0: 0 ════════════════════════╩════════════════════╬═══════╩══╬══╬═ -# ║ ║ ║ -# c_1: 0 ═════════════════════════════════════════════╬══════════╩══╬═ -# ║ ║ -# c_2: 0 ═════════════════════════════════════════════╬═════════════╩═ -# ║ -# c_3: 0 ═════════════════════════════════════════════╩═══════════════ -# -# -# 2 -# | -# 0 - 1 - 3 -# Build the expected output to verify the pass worked -expected = QuantumCircuit(qr, cr) -expected.cx(qr[1], qr[2]) -expected.h(qr[2]) -expected.swap(qr[0], qr[1]) -expected.h(qr[0]) -expected.cx(qr[1], qr[3]) -expected.h(qr[3]) -expected.measure(qr[1], cr[0]) -expected.swap(qr[1], qr[3]) -expected.cx(qr[2], qr[1]) -expected.h(qr[3]) -expected.swap(qr[0], qr[1]) -expected.measure(qr[2], cr[2]) -expected.cx(qr[3], qr[1]) -expected.measure(qr[0], cr[3]) -expected.measure(qr[3], cr[0]) -expected.measure(qr[1], cr[1]) -expected_dag = circuit_to_dag(expected) -# Run the pass on the dag from the input circuit -pass_ = StochasticSwap(coupling, 20, 999) -after = pass_.run(dag) -# Verify the output of the pass matches our expectation -assert expected_dag == after diff --git a/examples/python/teleport.py b/examples/python/teleport.py deleted file mode 100644 index 73d7fd61d22c..000000000000 --- a/examples/python/teleport.py +++ /dev/null @@ -1,82 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2017, 2023. -# -# 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 teleportation example. - -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 QuantumRegister, ClassicalRegister, QuantumCircuit -from qiskit import transpile -from qiskit.providers.basic_provider import BasicSimulator - -############################################################### -# Set the backend name and coupling map. -############################################################### -coupling_map = [[0, 1], [0, 2], [1, 2], [3, 2], [3, 4], [4, 2]] -backend = BasicSimulator() - -############################################################### -# Make a quantum program for quantum teleportation. -############################################################### -q = QuantumRegister(3, "q") -c0 = ClassicalRegister(1, "c0") -c1 = ClassicalRegister(1, "c1") -c2 = ClassicalRegister(1, "c2") -qc = QuantumCircuit(q, c0, c1, c2, name="teleport") - -# Prepare an initial state -qc.u(0.3, 0.2, 0.1, q[0]) - -# Prepare a Bell pair -qc.h(q[1]) -qc.cx(q[1], q[2]) - -# Barrier following state preparation -qc.barrier(q) - -# Measure in the Bell basis -qc.cx(q[0], q[1]) -qc.h(q[0]) -qc.measure(q[0], c0[0]) -qc.measure(q[1], c1[0]) - -# Apply a correction -qc.barrier(q) -qc.z(q[2]).c_if(c0, 1) -qc.x(q[2]).c_if(c1, 1) -qc.measure(q[2], c2[0]) - -############################################################### -# Execute. -# Experiment does not support feedback, so we use the simulator -############################################################### - -# First version: not mapped -initial_layout = {q[0]: 0, q[1]: 1, q[2]: 2} -job = backend.run( - transpile(qc, backend=backend, coupling_map=None, initial_layout=initial_layout), shots=1024 -) - -result = job.result() -print(result.get_counts(qc)) - -# Second version: mapped to 2x8 array coupling graph -job = backend.run( - transpile(qc, backend=backend, coupling_map=coupling_map, initial_layout=initial_layout), - shots=1024, -) -result = job.result() -print(result.get_counts(qc)) -# Both versions should give the same distribution diff --git a/examples/python/using_qiskit_terra_level_0.py b/examples/python/using_qiskit_terra_level_0.py deleted file mode 100644 index cd92701d22a2..000000000000 --- a/examples/python/using_qiskit_terra_level_0.py +++ /dev/null @@ -1,43 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2017, 2023. -# -# 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 at introduction level. - -This example shows the most basic way to use Qiskit. It builds some circuits -and runs them on the BasicProvider (local Qiskit provider). -""" - -# Import the Qiskit modules -from qiskit import QuantumCircuit -from qiskit import transpile -from qiskit.providers.basic_provider import BasicSimulator - -# 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]) - -# running the job -sim_backend = BasicSimulator() -job_sim = sim_backend.run(transpile([qc1, qc2], sim_backend)) -sim_result = job_sim.result() - -# Show the results -print(sim_result.get_counts(qc1)) -print(sim_result.get_counts(qc2)) diff --git a/examples/qasm/entangled_registers.qasm b/examples/qasm/entangled_registers.qasm deleted file mode 100644 index ad3f099342b8..000000000000 --- a/examples/qasm/entangled_registers.qasm +++ /dev/null @@ -1,20 +0,0 @@ -// A simple 8 qubit example entangling two 4 qubit registers -OPENQASM 2.0; -include "qelib1.inc"; - -qreg a[4]; -qreg b[4]; -creg c[4]; -creg d[4]; -h a; -cx a, b; -barrier a; -barrier b; -measure a[0]->c[0]; -measure a[1]->c[1]; -measure a[2]->c[2]; -measure a[3]->c[3]; -measure b[0]->d[0]; -measure b[1]->d[1]; -measure b[2]->d[2]; -measure b[3]->d[3]; diff --git a/examples/qasm/plaquette_check.qasm b/examples/qasm/plaquette_check.qasm deleted file mode 100644 index 9b5a6e94ef9e..000000000000 --- a/examples/qasm/plaquette_check.qasm +++ /dev/null @@ -1,21 +0,0 @@ -// plaquette check -OPENQASM 2.0; -include "qelib1.inc"; - -qreg q[5]; -creg c[5]; - -x q[1]; -x q[4]; -barrier q; - -cx q[0], q[2]; -cx q[1], q[2]; -cx q[3], q[2]; -cx q[4], q[2]; -barrier q; -measure q[0]->c[0]; -measure q[1]->c[1]; -measure q[2]->c[2]; -measure q[3]->c[3]; -measure q[4]->c[4]; diff --git a/examples/qasm/simple.qasm b/examples/qasm/simple.qasm deleted file mode 100644 index c034011d51f4..000000000000 --- a/examples/qasm/simple.qasm +++ /dev/null @@ -1,5 +0,0 @@ -include "qelib1.inc"; -qreg q[2]; - -h q[0]; -cx q[0],q[1]; diff --git a/test/python/test_examples.py b/test/python/test_examples.py deleted file mode 100644 index fc08ff3abaf7..000000000000 --- a/test/python/test_examples.py +++ /dev/null @@ -1,59 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2019. -# -# 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. - -"""Test examples scripts.""" - -import os -import subprocess -import sys -import unittest - -from test import QiskitTestCase - -examples_dir = os.path.abspath( - os.path.join( - os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "examples"), - "python", - ) -) -ibmq_examples_dir = os.path.join(examples_dir, "ibmq") - - -class TestPythonExamples(QiskitTestCase): - """Test example scripts""" - - @unittest.skipIf( - sys.platform == "darwin", - "Multiprocess spawn fails on macOS python >=3.8 without __name__ == '__main__' guard", - ) - def test_all_examples(self): - """Execute the example python files and pass if it returns 0.""" - examples = [] - if os.path.isdir(examples_dir): - examples = [x for x in os.listdir(examples_dir) if x.endswith(".py")] - for example in examples: - with self.subTest(example=example): - example_path = os.path.join(examples_dir, example) - cmd = [sys.executable, example_path] - with subprocess.Popen( - cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - env={**os.environ, "PYTHONIOENCODING": "utf8"}, - ) as run_example: - stdout, stderr = run_example.communicate() - error_string = ( - f"Running example {example} failed with return code" - f"{run_example.returncode}\n" - f"stdout:{stdout}\nstderr: {stderr}" - ) - self.assertEqual(run_example.returncode, 0, error_string) diff --git a/tox.ini b/tox.ini index a0c5665695d2..5456e7731920 100644 --- a/tox.ini +++ b/tox.ini @@ -39,15 +39,12 @@ basepython = python3 package = editable allowlist_externals = cargo commands = - black --check {posargs} qiskit test tools examples setup.py + black --check {posargs} qiskit test tools setup.py cargo fmt --check - ruff check qiskit test tools examples setup.py + ruff check qiskit test tools setup.py cargo clippy -- -D warnings pylint -rn qiskit test tools - # This line is commented out until #6649 merges. We can't run this currently - # via tox because tox doesn't support globbing - # pylint -rn --disable='invalid-name,missing-module-docstring,redefined-outer-name' examples/python/*.py - python {toxinidir}/tools/verify_headers.py qiskit test tools examples crates + python {toxinidir}/tools/verify_headers.py qiskit test tools crates python {toxinidir}/tools/find_optional_imports.py python {toxinidir}/tools/find_stray_release_notes.py reno -q lint @@ -56,12 +53,11 @@ commands = basepython = python3 allowlist_externals = git commands = - ruff check qiskit test tools examples setup.py - black --check {posargs} qiskit test tools examples setup.py + ruff check qiskit test tools setup.py + black --check {posargs} qiskit test tools setup.py -git fetch -q https://github.com/Qiskit/qiskit.git :lint_incr_latest python {toxinidir}/tools/pylint_incr.py -rn -j4 -sn --paths :/qiskit/*.py :/test/*.py :/tools/*.py - python {toxinidir}/tools/pylint_incr.py -rn -j4 -sn --disable='invalid-name,missing-module-docstring,redefined-outer-name' --paths :(glob,top)examples/python/*.py - python {toxinidir}/tools/verify_headers.py qiskit test tools examples crates + python {toxinidir}/tools/verify_headers.py qiskit test tools crates python {toxinidir}/tools/find_optional_imports.py python {toxinidir}/tools/find_stray_release_notes.py reno -q lint @@ -70,7 +66,7 @@ commands = skip_install = true deps = -r requirements-dev.txt -commands = black {posargs} qiskit test tools examples setup.py +commands = black {posargs} qiskit test tools setup.py [testenv:coverage] basepython = python3