def test_purity_mixed_state(self): state_1 = 0.5 * (projector(basis_state('0', 1)) + projector(basis_state('1', 1))) state_2 = (1 / 3.0) * (projector(basis_state('00', 2)) + projector( basis_state('01', 2)) + projector(basis_state('10', 2))) self.assertEqual(purity(state_1), 0.5) self.assertEqual(purity(state_2), 1.0 / 3)
def test_purity_basis_state_input(self): state_1 = basis_state('0', 1) state_2 = basis_state('11', 2) state_3 = basis_state('010', 3) self.assertEqual(purity(state_1), 1.0) self.assertEqual(purity(state_2), 1.0) self.assertEqual(purity(state_3), 1.0)
def test_basis_state_circuit(self): state = state = (basis_state('001', 3)+basis_state('111', 3))/np.sqrt(2) q = QuantumRegister(3) qc = QuantumCircuit(q) qc.initialize(state, [q[0], q[1], q[2]]) backend = Simulators.get_backend('statevector_simulator') qc_state = execute(qc, backend).result().get_statevector(qc) self.assertAlmostEqual(state_fidelity(qc_state, state), 1.0, places=7)
def test_basis_state_circuit(self): """TO BE REMOVED with qiskit.quantum_info.basis_state""" with self.assertWarns(DeprecationWarning): state = (basis_state('001', 3) + basis_state('111', 3))/np.sqrt(2) q = QuantumRegister(3) qc = QuantumCircuit(q) qc.initialize(state, [q[0], q[1], q[2]]) backend = BasicAer.get_backend('statevector_simulator') qc_state = execute(qc, backend).result().get_statevector(qc) self.assertAlmostEqual(state_fidelity(qc_state, state), 1.0, places=7)
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_purity_pure_state(self): state_1 = (1/np.sqrt(2))*(basis_state('0', 1) + basis_state('1', 1)) state_2 = (1/np.sqrt(3))*(basis_state('00', 2) + basis_state('01', 2) + basis_state('11', 2)) state_3 = 0.5*(basis_state('000', 3) + basis_state('001', 3) + basis_state('010', 3) + basis_state('100', 3)) self.assertEqual(purity(state_1), 1.0) self.assertEqual(purity(state_2), 1.0) self.assertEqual(purity(state_3), 1.0)
def test_purity_pure_matrix_state(self): state_1 = (1/np.sqrt(2))*(basis_state('0', 1) + basis_state('1', 1)) state_1 = projector(state_1) state_2 = (1/np.sqrt(3))*(basis_state('00', 2) + basis_state('01', 2) + basis_state('11', 2)) state_2 = projector(state_2) state_3 = 0.5*(basis_state('000', 3) + basis_state('001', 3) + basis_state('010', 3) + basis_state('100', 3)) state_3 = projector(state_3) self.assertAlmostEqual(purity(state_1), 1.0, places=10) self.assertAlmostEqual(purity(state_2), 1.0, places=10) self.assertEqual(purity(state_3), 1.0)
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 = QasmSimulator(silent=True) 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) target = {'01 10': 1024} result = backend_sim.run(qobj_circ).result() state = result.get_statevector(circ) self.assertEqual(counts, target) self.assertAlmostEqual(state_fidelity(basis_state('0110', 4), state), 1.0, places=7)
def test_basis(self): """TO BE REMOVED with qiskit.quantum_info.basis_state""" with self.assertWarns(DeprecationWarning): state = basis_state('010', 3) state_ideal = np.array([0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]) state_fidelity(state, state_ideal) self.assertEqual(state_fidelity(state, state_ideal), 1.0)
def test_basis(self): # reference state = basis_state('010', 3) state_ideal = np.array([ 0. + 0.j, 0. + 0.j, 1. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j, 0. + 0.j ]) state_fidelity(state, state_ideal) self.assertEqual(state_fidelity(state, state_ideal), 1.0)
def test_random_state(self): # this test that a random state converges to 1/d number = 100000 E_P0_last = 0 for ii in range(number): state = basis_state(bin(3)[2:].zfill(3), 3) E_P0 = (E_P0_last*ii)/(ii+1)+state_fidelity(state, random_state(2**3, seed=ii))/(ii+1) E_P0_last = E_P0 self.assertAlmostEqual(E_P0, 1/8, places=2)
def test_random_state(self): """TO BE REMOVED with qiskit.quantum_info.basis_state""" # this test that a random state converges to 1/d number = 1000 E_P0_last = 0 for ii in range(number): with self.assertWarns(DeprecationWarning): state = basis_state(bin(3)[2:].zfill(3), 3) E_P0 = (E_P0_last*ii)/(ii+1)+state_fidelity(state, random_state(2**3, seed=ii))/(ii+1) E_P0_last = E_P0 self.assertAlmostEqual(E_P0, 1/8, places=2)
def test_circuit_multi_case2(self): """Test circuit multi regs declared at start. """ qreg0 = QuantumRegister(2, 'q0') creg0 = ClassicalRegister(2, 'c0') qreg1 = QuantumRegister(2, 'q1') creg1 = ClassicalRegister(2, 'c1') circ2 = QuantumCircuit() circ2.add_register(qreg0) circ2.add_register(qreg1) circ2.x(qreg0[1]) circ2.x(qreg1[0]) meas2 = QuantumCircuit() meas2.add_register(qreg0) meas2.add_register(qreg1) meas2.add_register(creg0) meas2.add_register(creg1) meas2.measure(qreg0, creg0) meas2.measure(qreg1, creg1) qc2 = circ2 + meas2 backend_sim = Simulators.get_backend('statevector_simulator') result = execute(circ2, backend_sim).result() state = result.get_statevector(circ2) backend_sim = Simulators.get_backend('qasm_simulator') result = execute(qc2, backend_sim).result() counts = result.get_counts(qc2) backend_sim = Simulators.get_backend('unitary_simulator') result = execute(circ2, backend_sim).result() unitary = result.get_unitary(circ2) target = {'01 10': 1024} self.assertEqual(target, counts) self.assertAlmostEqual(state_fidelity(basis_state('0110', 4), state), 1.0, places=7) self.assertAlmostEqual(process_fidelity( Pauli(label='IXXI').to_matrix(), unitary), 1.0, places=7)
def test_lookup_rotation(self, reg_size, ref_rot): self.log.debug('Testing Lookup Rotation with positive eigenvalues') ref_sv_ampl = ref_rot**2 ref_size = reg_size + 3 # add work, msq and anc qubits ref_dim = 2**ref_size ref_sv = np.zeros(ref_dim, dtype=complex) ref_sv[int(ref_dim / 2) + 1] = ref_sv_ampl + 0j ref_sv[1] = np.sqrt(1 - ref_sv_ampl**2) + 0j state = basis_state('1', reg_size) a = QuantumRegister(reg_size, name='a') init_circuit = QuantumCircuit(a) init_circuit.initialize(state, a) lrot = LookupRotation(negative_evals=False) lrot_circuit = init_circuit + lrot.construct_circuit('', a) lrot_sv = sim_statevec(lrot_circuit) fidelity = state_fidelity(lrot_sv, ref_sv) np.testing.assert_approx_equal(fidelity, 1, significant=5) self.log.debug('Lookup rotation register size: {}'.format(reg_size)) self.log.debug('Lookup rotation fidelity: {}'.format(fidelity))
def test_lookup_rotation_neg(self, reg_size, ref_rot): """ lookup rotation neg test """ self.log.debug('Testing Lookup Rotation with support for negative ' 'eigenvalues') ref_sv_ampl = ref_rot**2 ref_size = reg_size + 3 # add work, msq and anc qubits ref_dim = 2**ref_size ref_sv = np.zeros(ref_dim, dtype=complex) ref_sv[int(ref_dim / 2) + 1] = -ref_sv_ampl + 0j ref_sv[1] = -np.sqrt(1 - ref_sv_ampl**2) + 0j state = basis_state('1', reg_size) q_a = QuantumRegister(reg_size, name='a') init_circuit = QuantumCircuit(q_a) init_circuit.initialize(state, q_a) lrot = LookupRotation(negative_evals=True) lrot_circuit = init_circuit + lrot.construct_circuit('', q_a) lrot_sv = _sim_statevec(lrot_circuit) fidelity = state_fidelity(lrot_sv, ref_sv) np.testing.assert_approx_equal(fidelity, 1, significant=5) self.log.debug('Lookup rotation register size: %s', reg_size) self.log.debug('Lookup rotation fidelity: %s', fidelity)
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 = qiskit.providers.aer.QasmSimulator() result = execute(qc, backend_sim, seed_mapper=34342).result() counts = result.get_counts(qc) target = {'01 10': 1024} backend_sim = qiskit.providers.aer.StatevectorSimulator() result = execute(circ, backend_sim, seed_mapper=3438).result() state = result.get_statevector(circ) backend_sim = qiskit.providers.aer.UnitarySimulator() result = execute(circ, backend_sim, seed_mapper=3438).result() unitary = result.get_unitary(circ) self.assertEqual(counts, target) self.assertAlmostEqual(state_fidelity(basis_state('0110', 4), state), 1.0, places=7) self.assertAlmostEqual(process_fidelity(Pauli(label='IXXI').to_matrix(), unitary), 1.0, places=7)