def test_online_simulators(self):
        """Test if there are online backends (which are simulators).

        If all correct some should exists. NEED internet connection for this.
        """
        # TODO: Jay should we check if we the QX is online before runing.
        qp = QuantumProgram(specs=QPS_SPECS)
        qp.set_api(API_TOKEN, URL)
        online_simulators = qp.online_simulators()
        # print(online_simulators)
        self.assertTrue(isinstance(online_simulators, list))
    def test_online_simulators(self):
        """Test if there are online backends (which are simulators).

        If all correct some should exists. NEED internet connection for this.
        """
        # TODO: Jay should we check if we the QX is online before runing.
        qp = QuantumProgram(specs=QPS_SPECS)
        qp.set_api(API_TOKEN, URL)
        online_simulators = qp.online_simulators()
        # print(online_simulators)
        self.assertTrue(isinstance(online_simulators, list))
 def test_execute_one_circuit_simulator_online(self):
     QP_program = QuantumProgram(specs=QPS_SPECS)
     qc = QP_program.get_circuit("circuitName")
     qr = QP_program.get_quantum_register("qname")
     cr = QP_program.get_classical_register("cname")
     qc.h(qr[1])
     qc.measure(qr[0], cr[0])
     shots = 1024  # the number of shots in the experiment.
     QP_program.set_api(API_TOKEN, URL)
     backend = QP_program.online_simulators()[0]
     # print(backend)
     result = QP_program.execute(['circuitName'], backend=backend,
                                 shots=shots, max_credits=3, silent=True)
     self.assertIsInstance(result, Result)
 def test_execute_one_circuit_simulator_online(self):
     QP_program = QuantumProgram(specs=QPS_SPECS)
     qc = QP_program.get_circuit("circuitName")
     qr = QP_program.get_quantum_register("qname")
     cr = QP_program.get_classical_register("cname")
     qc.h(qr[1])
     qc.measure(qr[0], cr[0])
     shots = 1024  # the number of shots in the experiment.
     QP_program.set_api(API_TOKEN, URL)
     backend = QP_program.online_simulators()[0]
     # print(backend)
     result = QP_program.execute(['circuitName'], backend=backend,
                                 shots=shots, max_credits=3, silent=True)
     self.assertIsInstance(result, Result)
 def test_execute_several_circuits_simulator_online(self):
     QP_program = QuantumProgram(specs=QPS_SPECS)
     qr = QP_program.get_quantum_register("qname")
     cr = QP_program.get_classical_register("cname")
     qc2 = QP_program.create_circuit("qc2", [qr], [cr])
     qc3 = QP_program.create_circuit("qc3", [qr], [cr])
     qc2.h(qr[0])
     qc3.h(qr[0])
     qc2.measure(qr[0], cr[0])
     qc3.measure(qr[0], cr[0])
     circuits = ['qc2', 'qc3']
     shots = 1024  # the number of shots in the experiment.
     QP_program.set_api(API_TOKEN, URL)
     backend = QP_program.online_simulators()[0]
     result = QP_program.execute(circuits, backend=backend, shots=shots,
                                 max_credits=3, silent=True)
     self.assertIsInstance(result, Result)
 def test_execute_several_circuits_simulator_online(self):
     QP_program = QuantumProgram(specs=QPS_SPECS)
     qr = QP_program.get_quantum_register("qname")
     cr = QP_program.get_classical_register("cname")
     qc2 = QP_program.create_circuit("qc2", [qr], [cr])
     qc3 = QP_program.create_circuit("qc3", [qr], [cr])
     qc2.h(qr[0])
     qc3.h(qr[0])
     qc2.measure(qr[0], cr[0])
     qc3.measure(qr[0], cr[0])
     circuits = ['qc2', 'qc3']
     shots = 1024  # the number of shots in the experiment.
     QP_program.set_api(API_TOKEN, URL)
     backend = QP_program.online_simulators()[0]
     result = QP_program.execute(circuits, backend=backend, shots=shots,
                                 max_credits=3, silent=True)
     self.assertIsInstance(result, Result)
    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(API_TOKEN, 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})
    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(API_TOKEN, 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})