|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2019 IBM. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# ============================================================================= |
| 17 | + |
| 18 | +from qiskit import execute, Aer, QuantumCircuit |
| 19 | +from qiskit.test.base import dicts_almost_equal |
| 20 | +from qiskit.transpiler.basepasses import AnalysisPass, TransformationPass |
| 21 | +from qiskit.test.mock import FakeMelbourne # NB will need to install dev requirements |
| 22 | +from qiskit.transpiler.passes import TrivialLayout |
| 23 | +from qiskit.transpiler import PassManager |
| 24 | +from qiskit.providers.aer.noise.device import basic_device_noise_model |
| 25 | + |
| 26 | + |
| 27 | +""" |
| 28 | +Your solution needs to comprise of one or more passes that you have written along with |
| 29 | +a PassManager that uses them. The PassManager is allowed to use passes that are already |
| 30 | +included in Qiskit. |
| 31 | +""" |
| 32 | + |
| 33 | + |
| 34 | +class MyBasicAnalysisPass(AnalysisPass): |
| 35 | + """Analysis passes look at the DAG to identify some property and then write this |
| 36 | + to the property set so that it can be accessed by other passes""" |
| 37 | + |
| 38 | + def run(self, dag): |
| 39 | + self.property_set['my depth'] = dag.depth() |
| 40 | + |
| 41 | + |
| 42 | +class MyBasicTransformationPass(TransformationPass): |
| 43 | + """Transformation passes alter the DAG and then return a DAG. They can use properties that |
| 44 | + have been written to the property set. |
| 45 | + """ |
| 46 | + def __init__(self, properties): |
| 47 | + self.properties = properties |
| 48 | + |
| 49 | + def run(self, dag): |
| 50 | + dag_depth = self.property_set['my depth'] |
| 51 | + gates = self.properties.gates |
| 52 | + return dag |
| 53 | + |
| 54 | + |
| 55 | +""" To test your passes you can use the fake backend classes. Your solutions will be tested against |
| 56 | +Yorktown, Ourense and Melbourne, as well as some internal backends. """ |
| 57 | +backend = FakeMelbourne() |
| 58 | +properties = backend.properties() |
| 59 | +coupling_map = backend.configuration().coupling_map |
| 60 | + |
| 61 | + |
| 62 | +""" You must submit a pass manager which uses at least one pass you have written. |
| 63 | +Examples of creating more complex pass managers can be seen in qiskit.transpiler.preset_passmanagers""" |
| 64 | +pass_manager = PassManager() |
| 65 | +pass_manager.append(TrivialLayout(coupling_map)) |
| 66 | +pass_manager.append(MyBasicAnalysisPass()) |
| 67 | +pass_manager.append(MyBasicTransformationPass(properties)) |
| 68 | + |
| 69 | +""" This allows us to simulate the noise a real device has, so that you don't have to wait for jobs to complete |
| 70 | +on the actual backends.""" |
| 71 | +noise_model = basic_device_noise_model(properties) |
| 72 | +simulator = Aer.get_backend('qasm_simulator') |
| 73 | + |
| 74 | + |
| 75 | +""" This is the circuit we are going to look at""" |
| 76 | +qc = QuantumCircuit(2, 2) |
| 77 | +qc.h(1) |
| 78 | +qc.measure(0, 0) |
| 79 | +circuits = [qc] |
| 80 | + |
| 81 | + |
| 82 | +result_normal = execute(circuits, |
| 83 | + simulator, |
| 84 | + coupling_map=coupling_map).result().get_counts() |
| 85 | + |
| 86 | +# NB we include the noise model in the second run |
| 87 | +result_noisy = execute(circuits, |
| 88 | + simulator, |
| 89 | + noise_model=noise_model, |
| 90 | + coupling_map=coupling_map).result().get_counts() |
| 91 | + |
| 92 | +""" Check to see how similar the counts from the two runs are, where delta the allowed difference between |
| 93 | +the counts. Returns an empty string if they are almost equal, otherwise returns an error message which can |
| 94 | +then be printed.""" |
| 95 | +equality_check = dicts_almost_equal(result_normal, result_noisy, delta=1e-8) |
| 96 | + |
| 97 | +if equality_check: |
| 98 | + print(equality_check) |
0 commit comments