当前位置: 首页>>代码示例>>Python>>正文


Python qiskit.transpile方法代码示例

本文整理汇总了Python中qiskit.transpile方法的典型用法代码示例。如果您正苦于以下问题:Python qiskit.transpile方法的具体用法?Python qiskit.transpile怎么用?Python qiskit.transpile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在qiskit的用法示例。


在下文中一共展示了qiskit.transpile方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_parameter_setter_from_automatic_repetition

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def test_parameter_setter_from_automatic_repetition(self, params):
        """Test getting and setting of the nlocal parameters."""
        circuit = QuantumCircuit(2)
        circuit.ry(Parameter('a'), 0)
        circuit.crx(Parameter('b'), 0, 1)

        # repeat circuit and check that parameters are duplicated
        reps = 3
        nlocal = NLocal(2, entanglement_blocks=circuit, reps=reps)
        nlocal.assign_parameters(params, inplace=True)

        param_set = set(p for p in params if isinstance(p, ParameterExpression))
        with self.subTest(msg='Test the parameters of the non-transpiled circuit'):
            # check the parameters of the final circuit
            self.assertEqual(nlocal.parameters, param_set)

        with self.subTest(msg='Test the parameters of the transpiled circuit'):
            basis_gates = ['id', 'u1', 'u2', 'u3', 'cx']
            transpiled_circuit = transpile(nlocal, basis_gates=basis_gates)
            self.assertEqual(transpiled_circuit.parameters, param_set) 
开发者ID:Qiskit,项目名称:qiskit-terra,代码行数:22,代码来源:test_library.py

示例2: test_parameters_setter

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def test_parameters_setter(self, params):
        """Test setting the parameters via list."""
        # construct circuit with some parameters
        initial_params = ParameterVector('p', length=6)
        circuit = QuantumCircuit(1)
        for i, initial_param in enumerate(initial_params):
            circuit.ry(i * initial_param, 0)

        # create an NLocal from the circuit and set the new parameters
        nlocal = NLocal(1, entanglement_blocks=circuit, reps=1)
        nlocal.assign_parameters(params, inplace=True)

        param_set = set(p for p in params if isinstance(p, ParameterExpression))
        with self.subTest(msg='Test the parameters of the non-transpiled circuit'):
            # check the parameters of the final circuit
            self.assertEqual(nlocal.parameters, param_set)

        with self.subTest(msg='Test the parameters of the transpiled circuit'):
            basis_gates = ['id', 'u1', 'u2', 'u3', 'cx']
            transpiled_circuit = transpile(nlocal, basis_gates=basis_gates)
            self.assertEqual(transpiled_circuit.parameters, param_set) 
开发者ID:Qiskit,项目名称:qiskit-terra,代码行数:23,代码来源:test_library.py

示例3: setup

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def setup(self, rb_pattern, _, __):
        length_vector = np.arange(1, 200, 4)
        nseeds = 1
        self.seed = 10
        self.circuits = build_rb_circuit(nseeds=nseeds,
                                         length_vector=length_vector,
                                         rb_pattern=rb_pattern,
                                         seed=self.seed)
        self.sim_backend = Aer.get_backend('qasm_simulator')
        trans_circ = transpile(self.circuits, backend=self.sim_backend,
                               seed_transpiler=self.seed)
        self.qobj = assemble(trans_circ, backend=self.sim_backend) 
开发者ID:Qiskit,项目名称:qiskit-aer,代码行数:14,代码来源:randomized_benchmarking.py

示例4: equivalent_transpile

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def equivalent_transpile(self, backend, opt_level):
        """Simulate, transpile and simulate the present circuit. Verify that the
        counts are not significantly different before and after transpilation.

        """

        print('Evaluating circuit at level {} on {}:\n{}'.format(
            opt_level, backend, self.qc.qasm()))

        assume(backend is None or backend.configuration().n_qubits >= len(self.qc.qubits))

        shots = 4096

        aer_counts = execute(self.qc, backend=self.backend,
                             shots=shots).result().get_counts()

        try:
            xpiled_qc = transpile(self.qc, backend=backend, optimization_level=opt_level)
        except Exception as e:
            failed_qasm = 'Exception caught during transpilation of circuit: \n{}'.format(
                self.qc.qasm())
            raise RuntimeError(failed_qasm) from e

        xpiled_aer_counts = execute(xpiled_qc, backend=self.backend,
                                    shots=shots).result().get_counts()

        count_differences = dicts_almost_equal(aer_counts, xpiled_aer_counts, 0.05 * shots)

        assert count_differences == '', 'Counts not equivalent: {}\nFailing QASM: \n{}'.format(
            count_differences, self.qc.qasm()) 
开发者ID:Qiskit,项目名称:qiskit-terra,代码行数:32,代码来源:test_transpiler_equivalence.py

示例5: test_cz_optimization

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def test_cz_optimization(self):
        """ Test that KAK does not run on a cz gate """
        qr = QuantumRegister(2)
        qc = QuantumCircuit(qr)

        qc.cz(qr[0], qr[1])

        cz_circ = transpile(qc, None, coupling_map=[[0, 1], [1, 0]],
                            basis_gates=['u1', 'u2', 'u3', 'id', 'cx'],
                            optimization_level=3)
        ops = cz_circ.count_ops()
        self.assertEqual(ops['u2'], 2)
        self.assertEqual(ops['cx'], 1)
        self.assertFalse('u3' in ops.keys()) 
开发者ID:Qiskit,项目名称:qiskit-terra,代码行数:16,代码来源:test_kak_over_optimization.py

示例6: test_cu1_optimization

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def test_cu1_optimization(self):
        """ Test that KAK does run on a cu1 gate and
        reduces the cx count from two to one.
        """
        qr = QuantumRegister(2)
        qc = QuantumCircuit(qr)

        qc.cu1(np.pi, qr[0], qr[1])

        cu1_circ = transpile(qc, None, coupling_map=[[0, 1], [1, 0]],
                             basis_gates=['u1', 'u2', 'u3', 'id', 'cx'],
                             optimization_level=3)
        ops = cu1_circ.count_ops()
        self.assertEqual(ops['cx'], 1) 
开发者ID:Qiskit,项目名称:qiskit-terra,代码行数:16,代码来源:test_kak_over_optimization.py

示例7: test_max_number_cnots

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def test_max_number_cnots(self):
        """
        Check if the number of cnots <= 2^(n+1) - 2n (arXiv:quant-ph/0406176)
        """
        num_qubits = 4
        _optimization_level = 0

        vector = np.array(
            [0.1314346 + 0.j, 0.32078572 - 0.01542775j, 0.13146466 + 0.0945312j,
             0.21090852 + 0.07935982j, 0.1700122 - 0.07905648j, 0.15570757 - 0.12309154j,
             0.18039667 + 0.04904504j, 0.22227187 - 0.05055569j, 0.23573255 - 0.09894111j,
             0.27307292 - 0.10372994j, 0.24162792 + 0.1090791j, 0.3115577 + 0.1211683j,
             0.1851788 + 0.08679141j, 0.36226463 - 0.09940202j, 0.13863395 + 0.10558225j,
             0.30767986 + 0.02073838j])

        vector = vector / np.linalg.norm(vector)

        qr = QuantumRegister(num_qubits, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.initialize(vector, qr)

        b = transpile(circuit, basis_gates=['u1', 'u2', 'u3', 'cx'],
                      optimization_level=_optimization_level)

        number_cnots = b.count_ops()['cx']
        max_cnots = 2 ** (num_qubits + 1) - 2 * num_qubits

        self.assertLessEqual(number_cnots, max_cnots) 
开发者ID:Qiskit,项目名称:qiskit-terra,代码行数:30,代码来源:test_initializer.py

示例8: assertCircuitEqual

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def assertCircuitEqual(self, qc1, qc2, visual=False, transpiled=True):
        """An equality test specialized to circuits."""
        if transpiled:
            basis_gates = ['id', 'u1', 'u3', 'cx']
            qc1_transpiled = transpile(qc1, basis_gates=basis_gates, optimization_level=0)
            qc2_transpiled = transpile(qc2, basis_gates=basis_gates, optimization_level=0)
            qc1, qc2 = qc1_transpiled, qc2_transpiled

        if visual:
            self.assertEqual(qc1.draw(), qc2.draw())
        else:
            self.assertEqual(qc1, qc2) 
开发者ID:Qiskit,项目名称:qiskit-terra,代码行数:14,代码来源:test_library.py

示例9: time_transpile_simulator_ripple_adder

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def time_transpile_simulator_ripple_adder(self, _, level):
        transpile(self.circuit, self.sim_backend,
                  optimization_level=level) 
开发者ID:Qiskit,项目名称:qiskit,代码行数:5,代码来源:ripple_adder.py

示例10: peakmem_transpile_simulator_ripple_adder

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def peakmem_transpile_simulator_ripple_adder(self, _, level):
        transpile(self.circuit, self.sim_backend,
                  optimization_level=level) 
开发者ID:Qiskit,项目名称:qiskit,代码行数:5,代码来源:ripple_adder.py

示例11: track_depth_transpile_simulator_ripple_adder

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def track_depth_transpile_simulator_ripple_adder(self, _, level):
        return transpile(self.circuit, self.sim_backend,
                         optimization_level=level).depth() 
开发者ID:Qiskit,项目名称:qiskit,代码行数:5,代码来源:ripple_adder.py

示例12: time_transpile_square_grid_ripple_adder

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def time_transpile_square_grid_ripple_adder(self, _, level):
        transpile(self.circuit,
                  coupling_map=self.coupling_map,
                  basis_gates=['u1', 'u2', 'u3', 'cx', 'id'],
                  optimization_level=level) 
开发者ID:Qiskit,项目名称:qiskit,代码行数:7,代码来源:ripple_adder.py

示例13: peakmem_transpile_square_grid_ripple_adder

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def peakmem_transpile_square_grid_ripple_adder(self, _, level):
        transpile(self.circuit,
                  coupling_map=self.coupling_map,
                  basis_gates=['u1', 'u2', 'u3', 'cx', 'id'],
                  optimization_level=level) 
开发者ID:Qiskit,项目名称:qiskit,代码行数:7,代码来源:ripple_adder.py

示例14: test_after_transpile

# 需要导入模块: import qiskit [as 别名]
# 或者: from qiskit import transpile [as 别名]
def test_after_transpile(self):
        """After transpile, the drawing should include the layout"""
        expected = '\n'.join([
            "                 ┌──────────┐┌──────────┐┌───┐┌──────────┐┌─┐   ",
            "   userqr_0 -> 0 ┤ U2(0,pi) ├┤ U2(0,pi) ├┤ X ├┤ U2(0,pi) ├┤M├───",
            "                 ├──────────┤├──────────┤└─┬─┘├──────────┤└╥┘┌─┐",
            "   userqr_1 -> 1 ┤ U2(0,pi) ├┤ U2(0,pi) ├──■──┤ U2(0,pi) ├─╫─┤M├",
            "                 └──────────┘└──────────┘     └──────────┘ ║ └╥┘",
            "  ancilla_0 -> 2 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "  ancilla_1 -> 3 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "  ancilla_2 -> 4 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "  ancilla_3 -> 5 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "  ancilla_4 -> 6 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "  ancilla_5 -> 7 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "  ancilla_6 -> 8 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "  ancilla_7 -> 9 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            " ancilla_8 -> 10 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            " ancilla_9 -> 11 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "ancilla_10 -> 12 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "ancilla_11 -> 13 ──────────────────────────────────────────╫──╫─",
            "                                                           ║  ║ ",
            "           c0_0: ══════════════════════════════════════════╩══╬═",
            "                                                              ║ ",
            "           c0_1: ═════════════════════════════════════════════╩═",
            "                                                                "])

        qr = QuantumRegister(2, 'userqr')
        cr = ClassicalRegister(2, 'c0')
        qc = QuantumCircuit(qr, cr)
        qc.h(qr)
        qc.cx(qr[0], qr[1])
        qc.measure(qr, cr)

        coupling_map = [[1, 0], [1, 2], [2, 3], [4, 3], [4, 10], [5, 4], [5, 6], [5, 9], [6, 8],
                        [7, 8], [9, 8], [9, 10], [11, 3], [11, 10], [11, 12], [12, 2], [13, 1],
                        [13, 12]]
        qc_result = transpile(qc, basis_gates=['u1', 'u2', 'u3', 'cx', 'id'],
                              coupling_map=coupling_map, optimization_level=0, seed_transpiler=0)
        self.assertEqual(qc_result.draw(output='text', cregbundle=False).single_string(), expected) 
开发者ID:Qiskit,项目名称:qiskit-terra,代码行数:52,代码来源:test_circuit_text_drawer.py


注:本文中的qiskit.transpile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。