mirror of https://github.com/Qiskit/qiskit.git
Convert two python examples into tests. (#147)
This commit is contained in:
parent
57e7dcd2c2
commit
aa30e206e8
|
@ -1,109 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2017 IBM RESEARCH. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# =============================================================================
|
||||
|
||||
"""
|
||||
Simple test of the mapper on an example that swaps a "1" state.
|
||||
"""
|
||||
|
||||
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
|
||||
|
||||
|
||||
###############################################################
|
||||
# Set the backend name and coupling map.
|
||||
###############################################################
|
||||
backend = "ibmqx_qasm_simulator"
|
||||
coupling_map = {0: [1, 8], 1: [2, 9], 2: [3, 10], 3: [4, 11], 4: [5, 12],
|
||||
5: [6, 13], 6: [7, 14], 7: [15], 8: [9], 9: [10], 10: [11],
|
||||
11: [12], 12: [13], 13: [14], 14: [15]}
|
||||
|
||||
###############################################################
|
||||
# Make a quantum program using some swap gates.
|
||||
###############################################################
|
||||
def swap(qc, q0, q1):
|
||||
"""Swap gate."""
|
||||
qc.cx(q0, q1)
|
||||
qc.cx(q1, q0)
|
||||
qc.cx(q0, q1)
|
||||
|
||||
|
||||
n = 3 # make this at least 3
|
||||
|
||||
QPS_SPECS = {
|
||||
"circuits": [{
|
||||
"name": "swapping",
|
||||
"quantum_registers": [
|
||||
{"name": "q",
|
||||
"size": n},
|
||||
{"name": "r",
|
||||
"size": n}
|
||||
],
|
||||
"classical_registers": [
|
||||
{"name": "ans",
|
||||
"size": 2*n},
|
||||
]}]
|
||||
}
|
||||
|
||||
qp = QuantumProgram(specs=QPS_SPECS)
|
||||
qc = qp.get_circuit("swapping")
|
||||
q = qp.get_quantum_register("q")
|
||||
r = qp.get_quantum_register("r")
|
||||
ans = qp.get_classical_register("ans")
|
||||
|
||||
# Set the first bit of q
|
||||
qc.x(q[0])
|
||||
|
||||
# Swap the set bit
|
||||
swap(qc, q[0], q[n-1])
|
||||
swap(qc, q[n-1], r[n-1])
|
||||
swap(qc, r[n-1], q[1])
|
||||
swap(qc, q[1], r[1])
|
||||
|
||||
# Insert a barrier before measurement
|
||||
qc.barrier()
|
||||
# Measure all of the qubits in the standard basis
|
||||
for j in range(n):
|
||||
qc.measure(q[j], ans[j])
|
||||
qc.measure(r[j], ans[j+n])
|
||||
|
||||
###############################################################
|
||||
# Set up the API and execute the program.
|
||||
###############################################################
|
||||
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
|
||||
# First version: no mapping
|
||||
result = qp.execute(["swapping"], backend=backend, coupling_map=None, shots=1024)
|
||||
print(result)
|
||||
print(result.get_ran_qasm("swapping"))
|
||||
print(result.get_counts("swapping"))
|
||||
|
||||
# Second version: map to coupling graph
|
||||
result = qp.execute(["swapping"], backend=backend, coupling_map=coupling_map, shots=1024)
|
||||
print(result)
|
||||
print(result.get_ran_qasm("swapping"))
|
||||
print(result.get_counts("swapping"))
|
||||
|
||||
# Both versions should give the same distribution
|
|
@ -1,109 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2017 IBM RESEARCH. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# =============================================================================
|
||||
|
||||
"""
|
||||
Illustrate compiling several circuits to different backends.
|
||||
"""
|
||||
|
||||
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
|
||||
|
||||
###############################################################
|
||||
# Set the backend name and coupling map.
|
||||
###############################################################
|
||||
backend = "ibmqx2"
|
||||
coupling_map = {0: [1, 2],
|
||||
1: [2],
|
||||
2: [],
|
||||
3: [2, 4],
|
||||
4: [2]}
|
||||
|
||||
###############################################################
|
||||
# Make a quantum program for the GHZ and Bell states.
|
||||
###############################################################
|
||||
QPS_SPECS = {
|
||||
"circuits": [{
|
||||
"name": "ghz",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": 5
|
||||
}],
|
||||
"classical_registers": [
|
||||
{"name": "c",
|
||||
"size": 5}
|
||||
]},{
|
||||
"name": "bell",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": 5
|
||||
}],
|
||||
"classical_registers": [
|
||||
{"name": "c",
|
||||
"size": 5
|
||||
}]}
|
||||
]
|
||||
}
|
||||
|
||||
qp = QuantumProgram(specs=QPS_SPECS)
|
||||
ghz = qp.get_circuit("ghz")
|
||||
bell = qp.get_circuit("bell")
|
||||
q = qp.get_quantum_register("q")
|
||||
c = qp.get_classical_register("c")
|
||||
|
||||
# Create a GHZ state
|
||||
ghz.h(q[0])
|
||||
for i in range(4):
|
||||
ghz.cx(q[i], q[i+1])
|
||||
# Insert a barrier before measurement
|
||||
ghz.barrier()
|
||||
# Measure all of the qubits in the standard basis
|
||||
for i in range(5):
|
||||
ghz.measure(q[i], c[i])
|
||||
|
||||
# Create a Bell state
|
||||
bell.h(q[0])
|
||||
bell.cx(q[0], q[1])
|
||||
bell.barrier()
|
||||
bell.measure(q[0], c[0])
|
||||
bell.measure(q[1], c[1])
|
||||
|
||||
print(ghz.qasm())
|
||||
print(bell.qasm())
|
||||
|
||||
###############################################################
|
||||
# Set up the API and execute the program.
|
||||
###############################################################
|
||||
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
|
||||
bellobj = qp.compile(["bell"], backend='local_qasm_simulator', shots=1024)
|
||||
ghzobj = qp.compile(["ghz"], backend='ibmqx_qasm_simulator', shots=1024,
|
||||
coupling_map=coupling_map)
|
||||
|
||||
bellresult = qp.run(bellobj)
|
||||
ghzresult = qp.run(ghzobj)
|
||||
|
||||
print(bellresult.get_counts("bell"))
|
||||
print(ghzresult.get_counts("ghz"))
|
|
@ -1073,6 +1073,137 @@ class TestQuantumProgram(unittest.TestCase):
|
|||
self.assertEqual(result.get_counts('new_circuit'),
|
||||
{'00': 505, '01': 519})
|
||||
|
||||
def test_example_multiple_compile(self):
|
||||
"""Test a toy example compiling multiple circuits.
|
||||
|
||||
Pass if the results are correct.
|
||||
"""
|
||||
coupling_map = {0: [1, 2],
|
||||
1: [2],
|
||||
2: [],
|
||||
3: [2, 4],
|
||||
4: [2]}
|
||||
QPS_SPECS = {
|
||||
"circuits": [{
|
||||
"name": "ghz",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": 5
|
||||
}],
|
||||
"classical_registers": [{
|
||||
"name": "c",
|
||||
"size": 5}
|
||||
]}, {
|
||||
"name": "bell",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": 5
|
||||
}],
|
||||
"classical_registers": [{
|
||||
"name": "c",
|
||||
"size": 5
|
||||
}]}
|
||||
]
|
||||
}
|
||||
qp = QuantumProgram(specs=QPS_SPECS)
|
||||
ghz = qp.get_circuit("ghz")
|
||||
bell = qp.get_circuit("bell")
|
||||
q = qp.get_quantum_register("q")
|
||||
c = qp.get_classical_register("c")
|
||||
# Create a GHZ state
|
||||
ghz.h(q[0])
|
||||
for i in range(4):
|
||||
ghz.cx(q[i], q[i+1])
|
||||
# Insert a barrier before measurement
|
||||
ghz.barrier()
|
||||
# Measure all of the qubits in the standard basis
|
||||
for i in range(5):
|
||||
ghz.measure(q[i], c[i])
|
||||
# Create a Bell state
|
||||
bell.h(q[0])
|
||||
bell.cx(q[0], q[1])
|
||||
bell.barrier()
|
||||
bell.measure(q[0], c[0])
|
||||
bell.measure(q[1], c[1])
|
||||
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
bellobj = qp.compile(["bell"], backend='local_qasm_simulator',
|
||||
shots=2048, seed=10)
|
||||
ghzobj = qp.compile(["ghz"], backend='local_qasm_simulator',
|
||||
shots=2048, coupling_map=coupling_map,
|
||||
seed=10)
|
||||
bellresult = qp.run(bellobj)
|
||||
ghzresult = qp.run(ghzobj)
|
||||
print(bellresult.get_counts("bell"))
|
||||
print(ghzresult.get_counts("ghz"))
|
||||
self.assertEqual(bellresult.get_counts("bell"),
|
||||
{'00000': 1034, '00011': 1014})
|
||||
self.assertEqual(ghzresult.get_counts("ghz"),
|
||||
{'00000': 1047, '11111': 1001})
|
||||
|
||||
def test_example_swap_bits(self):
|
||||
"""Test a toy example swapping a set bit around.
|
||||
|
||||
Uses the mapper. Pass if results are correct.
|
||||
"""
|
||||
backend = "ibmqx_qasm_simulator"
|
||||
coupling_map = {0: [1, 8], 1: [2, 9], 2: [3, 10], 3: [4, 11], 4: [5, 12],
|
||||
5: [6, 13], 6: [7, 14], 7: [15], 8: [9], 9: [10], 10: [11],
|
||||
11: [12], 12: [13], 13: [14], 14: [15]}
|
||||
def swap(qc, q0, q1):
|
||||
"""Swap gate."""
|
||||
qc.cx(q0, q1)
|
||||
qc.cx(q1, q0)
|
||||
qc.cx(q0, q1)
|
||||
n = 3 # make this at least 3
|
||||
QPS_SPECS = {
|
||||
"circuits": [{
|
||||
"name": "swapping",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": n},
|
||||
{"name": "r",
|
||||
"size": n}
|
||||
],
|
||||
"classical_registers": [
|
||||
{"name": "ans",
|
||||
"size": 2*n},
|
||||
]
|
||||
}]
|
||||
}
|
||||
qp = QuantumProgram(specs=QPS_SPECS)
|
||||
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
if backend not in qp.online_simulators():
|
||||
return
|
||||
qc = qp.get_circuit("swapping")
|
||||
q = qp.get_quantum_register("q")
|
||||
r = qp.get_quantum_register("r")
|
||||
ans = qp.get_classical_register("ans")
|
||||
# Set the first bit of q
|
||||
qc.x(q[0])
|
||||
# Swap the set bit
|
||||
swap(qc, q[0], q[n-1])
|
||||
swap(qc, q[n-1], r[n-1])
|
||||
swap(qc, r[n-1], q[1])
|
||||
swap(qc, q[1], r[1])
|
||||
# Insert a barrier before measurement
|
||||
qc.barrier()
|
||||
# Measure all of the qubits in the standard basis
|
||||
for j in range(n):
|
||||
qc.measure(q[j], ans[j])
|
||||
qc.measure(r[j], ans[j+n])
|
||||
# First version: no mapping
|
||||
result = qp.execute(["swapping"], backend=backend,
|
||||
coupling_map=None, shots=1024,
|
||||
seed=14)
|
||||
self.assertEqual(result.get_counts("swapping"),
|
||||
{'010000': 1024})
|
||||
# Second version: map to coupling graph
|
||||
result = qp.execute(["swapping"], backend=backend,
|
||||
coupling_map=coupling_map, shots=1024,
|
||||
seed=14)
|
||||
self.assertEqual(result.get_counts("swapping"),
|
||||
{'010000': 1024})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in New Issue