Ejemplo n.º 1
0
 def test_qasm_simulator(self):
     """Test data counts output for single circuit run against reference."""
     result = QasmSimulator(self.qobj).run()
     expected = {
         '100 100': 137,
         '011 011': 131,
         '101 101': 117,
         '111 111': 127,
         '000 000': 131,
         '010 010': 141,
         '110 110': 116,
         '001 001': 124
     }
     self.assertEqual(result.get_counts('test'), expected)
 def test_qasm_simulator(self):
     """Test data counts output for single circuit run against reference."""
     result = QasmSimulator().run(self.q_job)
     shots = 1024
     threshold = 0.025 * shots
     counts = result.get_counts('test')
     target = {
         '100 100': shots / 8,
         '011 011': shots / 8,
         '101 101': shots / 8,
         '111 111': shots / 8,
         '000 000': shots / 8,
         '010 010': shots / 8,
         '110 110': shots / 8,
         '001 001': shots / 8
     }
     self.assertDictAlmostEqual(counts, target, threshold)
 def test_qasm_simulator(self):
     """Test data counts output for single circuit run against reference."""
     result = QasmSimulator().run(self.q_job)
     expected = {'100 100': 137, '011 011': 131, '101 101': 117, '111 111': 127,
                 '000 000': 131, '010 010': 141, '110 110': 116, '001 001': 124}
     self.assertEqual(result.get_counts('test'), expected)
 def test_qasm_simulator_single_shot(self):
     """Test single shot run."""
     shots = 1
     self.qobj['config']['shots'] = shots
     result = QasmSimulator().run(self.q_job)
     self.assertEqual(result.get_status(), 'COMPLETED')
    def test_if_statement(self):
        self.log.info('test_if_statement_x')
        shots = 100
        max_qubits = 3
        qp = QuantumProgram()
        qr = qp.create_quantum_register('qr', max_qubits)
        cr = qp.create_classical_register('cr', max_qubits)
        circuit_if_true = qp.create_circuit('test_if_true', [qr], [cr])
        circuit_if_true.x(qr[0])
        circuit_if_true.x(qr[1])
        circuit_if_true.measure(qr[0], cr[0])
        circuit_if_true.measure(qr[1], cr[1])
        circuit_if_true.x(qr[2]).c_if(cr, 0x3)
        circuit_if_true.measure(qr[0], cr[0])
        circuit_if_true.measure(qr[1], cr[1])
        circuit_if_true.measure(qr[2], cr[2])
        circuit_if_false = qp.create_circuit('test_if_false', [qr], [cr])
        circuit_if_false.x(qr[0])
        circuit_if_false.measure(qr[0], cr[0])
        circuit_if_false.measure(qr[1], cr[1])
        circuit_if_false.x(qr[2]).c_if(cr, 0x3)
        circuit_if_false.measure(qr[0], cr[0])
        circuit_if_false.measure(qr[1], cr[1])
        circuit_if_false.measure(qr[2], cr[2])
        basis_gates = []  # unroll to base gates
        unroller = unroll.Unroller(
            qasm.Qasm(data=qp.get_qasm('test_if_true')).parse(),
            unroll.JsonBackend(basis_gates))
        ucircuit_true = unroller.execute()
        unroller = unroll.Unroller(
            qasm.Qasm(data=qp.get_qasm('test_if_false')).parse(),
            unroll.JsonBackend(basis_gates))
        ucircuit_false = unroller.execute()
        config = {'shots': shots, 'seed': self.seed}
        qobj = {'id': 'test_if_qobj',
                'config': {
                    'max_credits': 3,
                    'shots': shots,
                    'backend': 'local_qasm_simulator',
                },
                'circuits': [
                    {
                        'name': 'test_if_true',
                        'compiled_circuit': ucircuit_true,
                        'compiled_circuit_qasm': None,
                        'config': {'coupling_map': None,
                                   'basis_gates': 'u1,u2,u3,cx,id',
                                   'layout': None,
                                   'seed': None
                                   }
                    },
                    {
                        'name': 'test_if_false',
                        'compiled_circuit': ucircuit_false,
                        'compiled_circuit_qasm': None,
                        'config': {'coupling_map': None,
                                   'basis_gates': 'u1,u2,u3,cx,id',
                                   'layout': None,
                                   'seed': None
                                   }
                    }
                ]
        }
        q_job = QuantumJob(qobj, preformatted=True)
        result = QasmSimulator().run(q_job)
        result_if_true = result.get_data('test_if_true')
        self.log.info('result_if_true circuit:')
        self.log.info(circuit_if_true.qasm())
        self.log.info('result_if_true={0}'.format(result_if_true))

        result_if_false = result.get_data('test_if_false')        
        self.log.info('result_if_false circuit:')
        self.log.info(circuit_if_false.qasm())
        self.log.info('result_if_false={0}'.format(result_if_false))
        self.assertTrue(result_if_true['counts']['111'] == 100)
        self.assertTrue(result_if_false['counts']['001'] == 100)
Ejemplo n.º 6
0
 def test_qasm_simulator_single_shot(self):
     """Test single shot run."""
     shots = 1
     self.qobj['config']['shots'] = shots
     result = QasmSimulator(self.qobj).run()
     self.assertEqual(result.get_status(), 'COMPLETED')
Ejemplo n.º 7
0
    def test_if_statement(self):
        self.log.info('test_if_statement_x')
        shots = 100
        max_qubits = 3
        qp = QuantumProgram()
        qr = qp.create_quantum_register('qr', max_qubits)
        cr = qp.create_classical_register('cr', max_qubits)
        circuit_if_true = qp.create_circuit('test_if_true', [qr], [cr])
        circuit_if_true.x(qr[0])
        circuit_if_true.x(qr[1])
        circuit_if_true.measure(qr[0], cr[0])
        circuit_if_true.measure(qr[1], cr[1])
        circuit_if_true.x(qr[2]).c_if(cr, 0x3)
        circuit_if_true.measure(qr[0], cr[0])
        circuit_if_true.measure(qr[1], cr[1])
        circuit_if_true.measure(qr[2], cr[2])
        circuit_if_false = qp.create_circuit('test_if_false', [qr], [cr])
        circuit_if_false.x(qr[0])
        circuit_if_false.measure(qr[0], cr[0])
        circuit_if_false.measure(qr[1], cr[1])
        circuit_if_false.x(qr[2]).c_if(cr, 0x3)
        circuit_if_false.measure(qr[0], cr[0])
        circuit_if_false.measure(qr[1], cr[1])
        circuit_if_false.measure(qr[2], cr[2])
        basis_gates = []  # unroll to base gates
        unroller = unroll.Unroller(
            qasm.Qasm(data=qp.get_qasm('test_if_true')).parse(),
            unroll.JsonBackend(basis_gates))
        ucircuit_true = unroller.execute()
        unroller = unroll.Unroller(
            qasm.Qasm(data=qp.get_qasm('test_if_false')).parse(),
            unroll.JsonBackend(basis_gates))
        ucircuit_false = unroller.execute()
        config = {'shots': shots, 'seed': self.seed}
        qobj = {
            'id':
            'test_if_qobj',
            'config': {
                'max_credits': 3,
                'shots': shots,
                'backend': 'local_qasm_simulator',
            },
            'circuits': [{
                'name': 'test_if_true',
                'compiled_circuit': ucircuit_true,
                'compiled_circuit_qasm': None,
                'config': {
                    'coupling_map': None,
                    'basis_gates': 'u1,u2,u3,cx,id',
                    'layout': None,
                    'seed': None
                }
            }, {
                'name': 'test_if_false',
                'compiled_circuit': ucircuit_false,
                'compiled_circuit_qasm': None,
                'config': {
                    'coupling_map': None,
                    'basis_gates': 'u1,u2,u3,cx,id',
                    'layout': None,
                    'seed': None
                }
            }]
        }

        result = QasmSimulator(qobj).run()
        result_if_true = result.get_data('test_if_true')
        self.log.info('result_if_true circuit:')
        self.log.info(circuit_if_true.qasm())
        self.log.info('result_if_true={0}'.format(result_if_true))

        result_if_false = result.get_data('test_if_false')
        self.log.info('result_if_false circuit:')
        self.log.info(circuit_if_false.qasm())
        self.log.info('result_if_false={0}'.format(result_if_false))
        self.assertTrue(result_if_true['counts']['111'] == 100)
        self.assertTrue(result_if_false['counts']['001'] == 100)