def test_circuit_multi(self):
        """Test circuit multi regs declared at start.
        """
        qreg0 = QuantumRegister(2, 'q0')
        creg0 = ClassicalRegister(2, 'c0')
        qreg1 = QuantumRegister(2, 'q1')
        creg1 = ClassicalRegister(2, 'c1')
        circ = QuantumCircuit(qreg0, qreg1)
        circ.x(qreg0[1])
        circ.x(qreg1[0])

        meas = QuantumCircuit(qreg0, qreg1, creg0, creg1)
        meas.measure(qreg0, creg0)
        meas.measure(qreg1, creg1)

        qc = circ + meas

        backend_sim = Simulators.get_backend('qasm_simulator')
        qobj_qc = compile(qc, backend_sim, seed_mapper=34342)
        qobj_circ = compile(circ, backend_sim, seed_mapper=3438)

        result = backend_sim.run(qobj_qc).result()
        counts = result.get_counts(qc)

        backend_sim = LegacySimulators.get_backend('qasm_simulator')
        result = backend_sim.run(qobj_qc).result()
        counts_py = result.get_counts(qc)

        target = {'01 10': 1024}

        backend_sim = LegacySimulators.get_backend('statevector_simulator')
        result = backend_sim.run(qobj_circ).result()
        state = result.get_statevector(circ)

        backend_sim = Simulators.get_backend('statevector_simulator')
        result = backend_sim.run(qobj_circ).result()
        state_py = result.get_statevector(circ)

        backend_sim = Simulators.get_backend('unitary_simulator')
        result = backend_sim.run(qobj_circ).result()
        unitary = result.get_unitary(circ)

        self.assertEqual(counts, target)
        self.assertEqual(counts_py, target)
        self.assertAlmostEqual(state_fidelity(basis_state('0110', 4), state),
                               1.0,
                               places=7)
        self.assertAlmostEqual(state_fidelity(basis_state('0110', 4),
                                              state_py),
                               1.0,
                               places=7)
        self.assertAlmostEqual(process_fidelity(
            Pauli(label='IXXI').to_matrix(), unitary),
                               1.0,
                               places=7)
 def test_builtin_qasm_simulator(self):
     backend = LegacySimulators.get_backend('qasm_simulator')
     qobj = compile(self.circuits, backend=backend)
     exp = qobj.experiments[0]
     c_qasm = exp.header.compiled_circuit_qasm
     self.assertIn(self.qr_name, map(lambda x: x[0],
                                     exp.header.qubit_labels))
     self.assertIn(self.qr_name, c_qasm)
     self.assertIn(self.cr_name, map(lambda x: x[0],
                                     exp.header.clbit_labels))
     self.assertIn(self.cr_name, c_qasm)
Beispiel #3
0
def get_aer_backend(backend_name):
    try:
        backend = LegacySimulators.get_backend(backend_name)
        logger.debug('Using LegacySimulators backend {}.'.format(backend_name))
        return backend
    except:
        pass

    backend = BasicAer.get_backend(backend_name)
    logger.debug('Using BasicAer backend {}.'.format(backend_name))
    return backend
    def test_statevector_simulator(self):
        """Test final state vector for single circuit run."""
        result = execute(self.q_circuit,
                         backend=LegacySimulators.get_backend(
                             'statevector_simulator')).result()
        self.assertEqual(result.success, True)
        actual = result.get_statevector(self.q_circuit)

        # state is 1/sqrt(2)|00> + 1/sqrt(2)|11>, up to a global phase
        self.assertAlmostEqual((abs(actual[0]))**2, 1 / 2)
        self.assertEqual(actual[1], 0)
        self.assertEqual(actual[2], 0)
        self.assertAlmostEqual((abs(actual[3]))**2, 1 / 2)
Beispiel #5
0
    def test_statevector(self):
        """statevector from a bell state"""
        qr = qiskit.QuantumRegister(2)
        circuit = qiskit.QuantumCircuit(qr)
        circuit.h(qr[0])
        circuit.cx(qr[0], qr[1])

        sim_cpp = LegacySimulators.get_backend('statevector_simulator')
        sim_py = Simulators.get_backend('statevector_simulator')
        result_cpp = execute(circuit, sim_cpp).result()
        result_py = execute(circuit, sim_py).result()
        statevector_cpp = result_cpp.get_statevector()
        statevector_py = result_py.get_statevector()
        fidelity = state_fidelity(statevector_cpp, statevector_py)
        self.assertGreater(
            fidelity, self._desired_fidelity,
            "cpp vs. py statevector has low fidelity{0:.2g}.".format(fidelity))
Beispiel #6
0
    def test_qasm(self):
        """counts from a GHZ state"""
        qr = qiskit.QuantumRegister(3)
        cr = qiskit.ClassicalRegister(3)
        circuit = qiskit.QuantumCircuit(qr, cr)
        circuit.h(qr[0])
        circuit.cx(qr[0], qr[1])
        circuit.cx(qr[1], qr[2])
        circuit.measure(qr, cr)

        sim_cpp = LegacySimulators.get_backend('qasm_simulator')
        sim_py = Simulators.get_backend('qasm_simulator')
        shots = 2000
        result_cpp = execute(circuit, sim_cpp, shots=shots).result()
        result_py = execute(circuit, sim_py, shots=shots).result()
        counts_cpp = result_cpp.get_counts()
        counts_py = result_py.get_counts()
        self.assertDictAlmostEqual(counts_cpp, counts_py, shots * 0.08)
Beispiel #7
0
    def test_noise(self):
        """turn on a pauli x noise for qubits 0 and 2"""
        qr = qiskit.QuantumRegister(3)
        cr = qiskit.ClassicalRegister(3)
        circuit = qiskit.QuantumCircuit(qr, cr)
        circuit.iden(qr[0])
        circuit.noise(0)
        circuit.iden(qr[1])
        circuit.noise(1)
        circuit.iden(qr[2])
        circuit.measure(qr, cr)

        config = {'noise_params': {'id': {'p_pauli': [1.0, 0.0, 0.0]}}}
        sim = LegacySimulators.get_backend('qasm_simulator')
        shots = 1000
        result = execute(circuit, sim, config=config, shots=shots).result()
        counts = result.get_counts()
        target = {'101': shots}
        self.assertEqual(counts, target)
Beispiel #8
0
    def test_save_load(self):
        """save |+>|0>, do some stuff, then load"""
        qr = qiskit.QuantumRegister(2)
        cr = qiskit.ClassicalRegister(2)
        circuit = qiskit.QuantumCircuit(qr, cr)
        circuit.h(qr[0])
        circuit.save(1)
        circuit.cx(qr[0], qr[1])
        circuit.cx(qr[1], qr[0])
        circuit.h(qr[1])
        circuit.load(1)

        sim = LegacySimulators.get_backend('statevector_simulator')
        result = execute(circuit, sim).result()
        statevector = result.get_statevector()
        target = [0.70710678 + 0.j, 0.70710678 + 0.j, 0. + 0.j, 0. + 0.j]
        fidelity = state_fidelity(statevector, target)
        self.assertGreater(
            fidelity, self._desired_fidelity,
            "save-load statevector has low fidelity{0:.2g}.".format(fidelity))
Beispiel #9
0
    def test_snapshot(self):
        """snapshot a bell state in the middle of circuit"""
        qr = qiskit.QuantumRegister(2)
        cr = qiskit.ClassicalRegister(2)
        circuit = qiskit.QuantumCircuit(qr, cr)
        circuit.h(qr[0])
        circuit.cx(qr[0], qr[1])
        circuit.snapshot('3')
        circuit.cx(qr[0], qr[1])
        circuit.h(qr[1])

        sim = LegacySimulators.get_backend('statevector_simulator')
        result = execute(circuit, sim).result()
        # TODO: rely on Result.get_statevector() postprocessing rather than manual
        snapshots = result.data(0)['snapshots']['statevector']['3']
        snapshot = format_statevector(snapshots[0])
        target = [0.70710678 + 0.j, 0. + 0.j, 0. + 0.j, 0.70710678 + 0.j]
        fidelity = state_fidelity(snapshot, target)
        self.assertGreater(
            fidelity, self._desired_fidelity,
            "snapshot has low fidelity{0:.2g}.".format(fidelity))
Beispiel #10
0
    def test_qasm_reset_measure(self):
        """counts from a qasm program with measure and reset in the middle"""
        qr = qiskit.QuantumRegister(3)
        cr = qiskit.ClassicalRegister(3)
        circuit = qiskit.QuantumCircuit(qr, cr)
        circuit.h(qr[0])
        circuit.cx(qr[0], qr[1])
        circuit.reset(qr[0])
        circuit.cx(qr[1], qr[2])
        circuit.t(qr)
        circuit.measure(qr[1], cr[1])
        circuit.h(qr[2])
        circuit.measure(qr[2], cr[2])

        sim_cpp = LegacySimulators.get_backend('qasm_simulator')
        sim_py = Simulators.get_backend('qasm_simulator')
        shots = 1000
        result_cpp = execute(circuit, sim_cpp, shots=shots, seed=1).result()
        result_py = execute(circuit, sim_py, shots=shots, seed=1).result()
        counts_cpp = result_cpp.get_counts()
        counts_py = result_py.get_counts()
        self.assertDictAlmostEqual(counts_cpp, counts_py, shots * 0.06)
from qiskit import LegacySimulators
from qiskit.tools.visualization import plot_histogram
from qiskit_aqua import QuantumInstance
from qiskit_aqua import run_algorithm
from qiskit_aqua.algorithms import Grover
from qiskit_aqua.components.oracles import SAT

with open('3sat3-5.cnf', 'r') as f:
    sat_cnf = f.read()
print(sat_cnf)

sat_oracle = SAT(sat_cnf)

grover = Grover(sat_oracle)

backend = LegacySimulators.get_backend('qasm_simulator')
quantum_instance = QuantumInstance(backend, shots=100)
result = grover.run(quantum_instance)
print(result['result'])

plot_histogram(result['measurements'])

params = {
    'problem': {
        'name': 'search'
    },
    'algorithm': {
        'name': 'Grover'
    },
    'oracle': {
        'name': 'SAT',
Beispiel #12
0
 def test_legacy_deprecated(self):
     """test deprecated legacy simulators backends are resolved correctly"""
     old_name = 'local_qiskit_simulator'
     new_backend = LegacySimulators.get_backend(old_name)
     self.assertIsInstance(new_backend, QasmSimulator)