Пример #1
0
    def test_from_backend(self):
        """Test whether a local mitigator can be created directly from backend properties"""
        backend = FakeYorktown()
        num_qubits = len(backend.properties().qubits)
        rng = np.random.default_rng(42)
        probs = rng.random((num_qubits, 2))
        for qubit_idx, qubit_prop in enumerate(backend.properties().qubits):
            for prop in qubit_prop:
                if prop.name == "prob_meas1_prep0":
                    prop.value = probs[qubit_idx][0]
                if prop.name == "prob_meas0_prep1":
                    prop.value = probs[qubit_idx][1]
        LRM_from_backend = LocalReadoutMitigator(backend=backend)

        mats = []
        for qubit_idx in range(num_qubits):
            mat = np.array([
                [1 - probs[qubit_idx][0], probs[qubit_idx][1]],
                [probs[qubit_idx][0], 1 - probs[qubit_idx][1]],
            ])
            mats.append(mat)
        LRM_from_matrices = LocalReadoutMitigator(amats=mats)
        self.assertTrue(
            matrix_equal(LRM_from_backend.assignment_matrix(),
                         LRM_from_matrices.assignment_matrix()))
Пример #2
0
 def test_no_limits_with_negative(self):
     """Test that we're not enforcing a trial limit if set to negative."""
     backend = FakeYorktown()
     qc = QuantumCircuit(3)
     qc.h(0)
     cmap = CouplingMap(backend.configuration().coupling_map)
     implicit_max = len(cmap.graph.edge_list()) + 15
     properties = backend.properties()
     # Run without any limits set
     vf2_pass = VF2Layout(cmap,
                          properties=properties,
                          seed=42,
                          max_trials=0)
     property_set = {}
     with self.assertLogs("qiskit.transpiler.passes.layout.vf2_layout",
                          level="DEBUG") as cm:
         vf2_pass(qc, property_set)
     for output in cm.output:
         self.assertNotIn("is >= configured max trials", output)
     last_line = cm.output[-1]
     # The last line should be
     # DEBUG:qiskit.transpiler.passes.layout.vf2_layout: Trial n has score 0.122
     trials = int(last_line.split(" ")[1])
     self.assertGreater(trials, implicit_max)
     self.assertEqual(set(property_set["layout"].get_physical_bits()),
                      {3, 1, 0})
Пример #3
0
    def test_time_limit_exceeded(self):
        """Test the pass stops after time_limit is reached."""
        backend = FakeYorktown()
        qr = QuantumRegister(2)
        qc = QuantumCircuit(qr)
        qc.x(qr)
        qc.measure_all()
        cmap = CouplingMap(backend.configuration().coupling_map)
        properties = backend.properties()
        vf2_pass = VF2Layout(cmap,
                             properties=properties,
                             seed=-1,
                             time_limit=0.0)
        property_set = {}
        with self.assertLogs("qiskit.transpiler.passes.layout.vf2_layout",
                             level="DEBUG") as cm:
            vf2_pass(qc, property_set)
        for output in cm.output:
            if output.startswith(
                    "DEBUG:qiskit.transpiler.passes.layout.vf2_layout:VF2Layout has taken"
            ) and output.endswith("which exceeds configured max time: 0.0"):
                break
        else:
            self.fail("No failure debug log message found")

        self.assertEqual(set(property_set["layout"].get_physical_bits()),
                         {2, 0})
Пример #4
0
 def test_with_properties(self):
     """Test scores with properties."""
     backend = FakeYorktown()
     cmap = CouplingMap(backend.configuration().coupling_map)
     properties = backend.properties()
     vf2_pass = VF2Layout(cmap, properties=properties)
     qr = QuantumRegister(2)
     layout = Layout({qr[0]: 4, qr[1]: 2})
     bad_score = vf2_pass._score_layout(layout)
     self.assertAlmostEqual(0.4075, bad_score)
     better_layout = Layout({qr[0]: 1, qr[1]: 3})
     better_score = vf2_pass._score_layout(better_layout)
     self.assertAlmostEqual(0.0588, better_score)
Пример #5
0
 def test_with_properties(self):
     """Test it finds the least noise perfect layout with no properties."""
     backend = FakeYorktown()
     qr = QuantumRegister(2)
     qc = QuantumCircuit(qr)
     qc.x(qr)
     qc.measure_all()
     cmap = CouplingMap(backend.configuration().coupling_map)
     properties = backend.properties()
     vf2_pass = VF2Layout(cmap, properties=properties)
     property_set = {}
     vf2_pass(qc, property_set)
     self.assertEqual(set(property_set["layout"].get_physical_bits()),
                      {1, 3})
Пример #6
0
 def test_max_trials_exceeded(self):
     """Test it exits when max_trials is reached."""
     backend = FakeYorktown()
     qr = QuantumRegister(2)
     qc = QuantumCircuit(qr)
     qc.x(qr)
     qc.measure_all()
     cmap = CouplingMap(backend.configuration().coupling_map)
     properties = backend.properties()
     vf2_pass = VF2Layout(cmap,
                          properties=properties,
                          seed=-1,
                          max_trials=1)
     property_set = {}
     with self.assertLogs("qiskit.transpiler.passes.layout.vf2_layout",
                          level="DEBUG") as cm:
         vf2_pass(qc, property_set)
     self.assertIn(
         "DEBUG:qiskit.transpiler.passes.layout.vf2_layout:Trial 1 is >= configured max trials 1",
         cm.output,
     )
     self.assertEqual(set(property_set["layout"].get_physical_bits()),
                      {2, 0})