def test_two_unitary_simulator(self): """test running two circuits This test is similar to one in test_quantumprogram but doesn't use multiprocessing. """ qr = QuantumRegister(2) cr = ClassicalRegister(1) qc1 = QuantumCircuit(qr, cr) qc2 = QuantumCircuit(qr, cr) qc1.h(qr) qc2.cx(qr[0], qr[1]) backend = UnitarySimulatorPy() qobj = compile([qc1, qc2], backend=backend) job = backend.run(QuantumJob(qobj, backend=backend, preformatted=True)) unitary1 = job.result().get_unitary(qc1) unitary2 = job.result().get_unitary(qc2) unitaryreal1 = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, -0.5, 0.5, -0.5], [0.5, 0.5, -0.5, -0.5], [0.5, -0.5, -0.5, 0.5]]) unitaryreal2 = np.array([[1, 0, 0, 0], [0, 0, 0, 1], [0., 0, 1, 0], [0, 1, 0, 0]]) norm1 = np.trace(np.dot(np.transpose(np.conj(unitaryreal1)), unitary1)) norm2 = np.trace(np.dot(np.transpose(np.conj(unitaryreal2)), unitary2)) self.assertAlmostEqual(norm1, 4) self.assertAlmostEqual(norm2, 4)
def test_local_unitary_simulator_single_thread(self): """test running two circuits Identical to the above test, but does not use multiprocessing. """ qr = QuantumRegister(2) cr = ClassicalRegister(1) qc1 = QuantumCircuit(qr, cr) qc2 = QuantumCircuit(qr, cr) qc1.h(qr) qc2.cx(qr[0], qr[1]) backend = UnitarySimulatorPy() qobj = compile([qc1, qc2], backend=backend) job = backend.run(qobj) unitary1 = job.result().get_unitary(qc1) unitary2 = job.result().get_unitary(qc2) unitaryreal1 = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, -0.5, 0.5, -0.5], [0.5, 0.5, -0.5, -0.5], [0.5, -0.5, -0.5, 0.5]]) unitaryreal2 = np.array([[1, 0, 0, 0], [0, 0, 0, 1], [0., 0, 1, 0], [0, 1, 0, 0]]) norm1 = np.trace(np.dot(np.transpose(np.conj(unitaryreal1)), unitary1)) norm2 = np.trace(np.dot(np.transpose(np.conj(unitaryreal2)), unitary2)) self.assertAlmostEqual(norm1, 4) self.assertAlmostEqual(norm2, 4)
def test_local_unitary_simulator(self): """Test unitary simulator. If all correct should return the hxh and cx. """ qr = QuantumRegister(2) cr = ClassicalRegister(2) qc1 = QuantumCircuit(qr, cr) qc2 = QuantumCircuit(qr, cr) qc1.h(qr) qc2.cx(qr[0], qr[1]) backend = UnitarySimulatorPy() qobj = compile([qc1, qc2], backend=backend) job = backend.run(qobj) unitary1 = job.result().get_unitary(qc1) unitary2 = job.result().get_unitary(qc2) unitaryreal1 = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, -0.5, 0.5, -0.5], [0.5, 0.5, -0.5, -0.5], [0.5, -0.5, -0.5, 0.5]]) unitaryreal2 = np.array([[1, 0, 0, 0], [0, 0, 0, 1], [0., 0, 1, 0], [0, 1, 0, 0]]) norm1 = np.trace(np.dot(np.transpose(np.conj(unitaryreal1)), unitary1)) norm2 = np.trace(np.dot(np.transpose(np.conj(unitaryreal2)), unitary2)) self.assertAlmostEqual(norm1, 4) self.assertAlmostEqual(norm2, 4)
def test_two_unitary_simulator(self): """test running two circuits This test is similar to one in test_quantumprogram but doesn't use multiprocessing. """ qr = QuantumRegister(2, 'q') cr = ClassicalRegister(1, 'c') qc1 = QuantumCircuit(qr, cr) qc2 = QuantumCircuit(qr, cr) qc1.h(qr) qc2.cx(qr[0], qr[1]) circuits = [qc1, qc2] backend = UnitarySimulatorPy() quantum_job = QuantumJob(circuits, do_compile=False, backend=backend) result = backend.run(quantum_job).result() unitary1 = result[0]['data']['unitary'] unitary2 = result[1]['data']['unitary'] unitaryreal1 = np.array([[0.5, 0.5, 0.5, 0.5], [0.5, -0.5, 0.5, -0.5], [0.5, 0.5, -0.5, -0.5], [0.5, -0.5, -0.5, 0.5]]) unitaryreal2 = np.array([[1, 0, 0, 0], [0, 0, 0, 1], [0., 0, 1, 0], [0, 1, 0, 0]]) norm1 = np.trace(np.dot(np.transpose(np.conj(unitaryreal1)), unitary1)) norm2 = np.trace(np.dot(np.transpose(np.conj(unitaryreal2)), unitary2)) self.assertAlmostEqual(norm1, 4) self.assertAlmostEqual(norm2, 4)
def test_local_unitary_simulator(self): """Test unitary simulator.""" circuits = self._test_circuits() backend = UnitarySimulatorPy() qobj = compile(circuits, backend=backend) job = backend.run(qobj) sim_unitaries = [job.result().get_unitary(circ) for circ in circuits] reference_unitaries = self._reference_unitaries() norms = [ np.trace(np.dot(np.transpose(np.conj(target)), actual)) for target, actual in zip(reference_unitaries, sim_unitaries) ] for norm in norms: self.assertAlmostEqual(norm, 8)