How to use the qiskit.compiler.assemble function in qiskit

To help you get started, we’ve selected a few qiskit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Qiskit / qiskit-terra / test / python / compiler / test_compiler.py View on Github external
def test_compile_pass_manager(self):
        """Test compile with and without an empty pass manager."""
        qr = QuantumRegister(2)
        cr = ClassicalRegister(2)
        qc = QuantumCircuit(qr, cr)
        qc.u1(3.14, qr[0])
        qc.u2(3.14, 1.57, qr[0])
        qc.barrier(qr)
        qc.measure(qr, cr)
        backend = BasicAer.get_backend('qasm_simulator')
        qrtrue = assemble(transpile(qc, backend, seed_transpiler=8),
                          seed_simulator=42)
        rtrue = backend.run(qrtrue).result()
        qrfalse = assemble(transpile(qc, backend, seed_transpiler=8,
                                     pass_manager=PassManager()),
                           seed_simulator=42)
        rfalse = backend.run(qrfalse).result()
        self.assertEqual(rtrue.get_counts(), rfalse.get_counts())
github Qiskit / qiskit-terra / test / python / circuit / test_unitary.py View on Github external
def test_labeled_unitary(self):
        """test qobj output with unitary matrix"""
        qr = QuantumRegister(4)
        qc = QuantumCircuit(qr)
        sigmax = numpy.array([[0, 1], [1, 0]])
        sigmay = numpy.array([[0, -1j], [1j, 0]])
        matrix = numpy.kron(sigmax, sigmay)
        uni = UnitaryGate(matrix, label='xy')
        qc.append(uni, [qr[0], qr[1]])
        qobj = qiskit.compiler.assemble(qc)
        instr = qobj.experiments[0].instructions[0]
        self.assertEqual(instr.name, 'unitary')
        self.assertEqual(instr.label, 'xy')
github Qiskit / qiskit-ibmq-provider / test / ibmq / test_ibmq_qasm_simulator.py View on Github external
cr1 = ClassicalRegister(2)
        qr2 = QuantumRegister(2)
        cr2 = ClassicalRegister(2)
        qcr1 = QuantumCircuit(qr1, qr2, cr1, cr2, name="circuit1")
        qcr2 = QuantumCircuit(qr1, qr2, cr1, cr2, name="circuit2")
        qcr1.x(qr1[0])
        qcr2.x(qr2[1])
        qcr1.measure(qr1[0], cr1[0])
        qcr1.measure(qr1[1], cr1[1])
        qcr1.measure(qr2[0], cr2[0])
        qcr1.measure(qr2[1], cr2[1])
        qcr2.measure(qr1[0], cr1[0])
        qcr2.measure(qr1[1], cr1[1])
        qcr2.measure(qr2[0], cr2[0])
        qcr2.measure(qr2[1], cr2[1])
        qobj = assemble(transpile([qcr1, qcr2], self.sim_backend, seed_transpiler=8458),
                        backend=self.sim_backend, shots=1024)
        job = self.sim_backend.run(qobj, validate_qobj=True)
        result = job.result()
        result1 = result.get_counts(qcr1)
        result2 = result.get_counts(qcr2)
        self.assertEqual(result1, {'00 01': 1024})
        self.assertEqual(result2, {'10 00': 1024})
github Qiskit / qiskit-terra / test / python / circuit / test_parameters.py View on Github external
def test_circuit_generation(self):
        """Test creating a series of circuits parametrically"""
        theta = Parameter('θ')
        qr = QuantumRegister(1)
        qc = QuantumCircuit(qr)
        qc.rx(theta, qr)
        backend = BasicAer.get_backend('qasm_simulator')
        qc_aer = transpile(qc, backend)

        # generate list of circuits
        circs = []
        theta_list = numpy.linspace(0, numpy.pi, 20)
        for theta_i in theta_list:
            circs.append(qc_aer.bind_parameters({theta: theta_i}))
        qobj = assemble(circs)
        for index, theta_i in enumerate(theta_list):
            self.assertEqual(float(qobj.experiments[index].instructions[0].params[0]),
                             theta_i)
github Qiskit / qiskit-terra / test / python / transpiler / test_preset_passmanagers.py View on Github external
qr = QuantumRegister(3, 'q')
        cr = ClassicalRegister(3)
        ancilla = QuantumRegister(13, 'ancilla')
        qc = QuantumCircuit(qr, cr)
        qc.cx(qr[2], qr[1])
        qc.cx(qr[2], qr[0])
        initial_layout = {0: qr[1], 2: qr[0], 15: qr[2]}
        final_layout = {0: qr[1], 1: ancilla[0], 2: qr[0], 3: ancilla[1], 4: ancilla[2],
                        5: ancilla[3], 6: ancilla[4], 7: ancilla[5], 8: ancilla[6],
                        9: ancilla[7], 10: ancilla[8], 11: ancilla[9], 12: ancilla[10],
                        13: ancilla[11], 14: ancilla[12], 15: qr[2]}

        backend = FakeRueschlikon()

        qc_b = transpile(qc, backend, initial_layout=initial_layout, optimization_level=level)
        qobj = assemble(qc_b)

        self.assertEqual(qc_b._layout._p2v, final_layout)

        compiled_ops = qobj.experiments[0].instructions
        for operation in compiled_ops:
            if operation.name == 'cx':
                self.assertIn(operation.qubits, backend.configuration().coupling_map)
                self.assertIn(operation.qubits, [[15, 0], [15, 2]])
github Qiskit / qiskit-ibmq-provider / test / utils.py View on Github external
def bell_in_qobj(backend: IBMQBackend, shots: int = 1024) -> QasmQobj:
    """Return a bell circuit in Qobj format.

    Args:
        backend: Backend to use for transpiling the circuit.
        shots: Number of shots.

    Returns:
        A bell circuit in Qobj format.
    """
    return assemble(transpile(ReferenceCircuits.bell(), backend=backend),
                    backend=backend, shots=shots)
github Qiskit / qiskit-aer / test / terra / backends / qasm_simulator / qasm_method.py View on Github external
def test_backend_method_clifford_circuits_and_pauli_noise(self):
        """Test statevector method is used for Clifford circuit"""
        # Noise Model
        error = pauli_error([['XX', 0.5], ['II', 0.5]], standard_gates=True)
        noise_model = NoiseModel()
        noise_model.add_all_qubit_quantum_error(error, ['cz', 'cx'])

        # Test circuits
        shots = 100
        circuits = ref_2q_clifford.cz_gate_circuits_deterministic(
            final_measure=True)
        qobj = assemble(circuits, self.SIMULATOR, shots=shots)

        def get_result():
            return self.SIMULATOR.run(
                qobj,
                backend_options=self.BACKEND_OPTS,
                noise_model=noise_model).result()

        # Check simulation method
        method = self.BACKEND_OPTS.get('method', 'automatic')
        result = self.SIMULATOR.run(
            qobj, backend_options=self.BACKEND_OPTS).result()
        self.is_completed(result)
        if method != 'automatic':
            self.compare_result_metadata(result, circuits, 'method',
                                         method)
        else:
github Qiskit / qiskit-terra / test / python / qobj / test_qobj_identifiers.py View on Github external
def test_builtin_qasm_simulator(self):
        qobj = assemble(self.circuits)
        exp = qobj.experiments[0]
        self.assertIn(self.qr_name, map(lambda x: x[0], exp.header.qubit_labels))
        self.assertIn(self.cr_name, map(lambda x: x[0], exp.header.clbit_labels))
github Qiskit / qiskit-ibmq-provider / test / ibmq / test_ibmq_connector.py View on Github external
def test_api_run_job(self, qe_token, qe_url):
        """Test running a job against a simulator."""
        IBMQ.enable_account(qe_token, qe_url)

        backend_name = 'ibmq_qasm_simulator'
        backend = IBMQ.get_backend(backend_name)
        qobj = assemble(transpile(self.qc1, backend=backend, seed_transpiler=self.seed),
                        backend=backend, shots=1)

        api = backend._api
        job = api.submit_job(qobj.to_dict(), backend_name)
        check_status = None
        if 'status' in job:
            check_status = job['status']
        self.assertIsNotNone(check_status)
github Qiskit / qiskit-aer / test / terra / backends / qasm_simulator / qasm_snapshot.py View on Github external
def test_snapshot_expval_matrix_post_measure(self):
        """Test snapshot expectation value (matrix) after final measurement"""
        shots = 1000
        labels = snapshot_expval_labels()
        counts_targets = snapshot_expval_counts(shots)
        value_targets = snapshot_expval_post_meas_values()

        circuits = snapshot_expval_circuits(pauli=False, post_measure=True)

        qobj = assemble(circuits, self.SIMULATOR, shots=shots)
        job = self.SIMULATOR.run(qobj, backend_options=self.BACKEND_OPTS)
        result = job.result()
        success = getattr(result, 'success', False)
        method = self.BACKEND_OPTS.get('method', 'automatic')
        if method not in QasmSnapshotExpValMatrixTests.SUPPORTED_QASM_METHODS:
            self.assertFalse(success)
        else:
            self.assertTrue(success)
            self.compare_counts(result, circuits, counts_targets, delta=0.1 * shots)
            # Check snapshots
            for j, circuit in enumerate(circuits):
                data = result.data(circuit)
                all_snapshots = self.expval_snapshots(data, labels)
                for label in labels:
                    snaps = all_snapshots.get(label, {})
                    self.assertTrue(len(snaps), 1)