コード例 #1
0
 def test_sympy(self):
     desired_vector = [
         0,
         math.cos(math.pi / 3) * complex(0, 1) / math.sqrt(4),
         math.sin(math.pi / 3) / math.sqrt(4),
         0,
         0,
         0,
         0,
         0,
         1 / math.sqrt(8) * complex(1, 0),
         1 / math.sqrt(8) * complex(0, 1),
         0,
         0,
         0,
         0,
         1 / math.sqrt(4),
         1 / math.sqrt(4) * complex(0, 1)]
     qp = QuantumProgram()
     qr = QuantumRegister(4, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]])
     qp.add_circuit("qc", qc)
     result = qp.execute("qc", backend='local_statevector_simulator')
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #2
0
    def test_qasm_snapshot(self):
        """snapshot a circuit at multiple places"""
        qr = qiskit.QuantumRegister(3)
        cr = qiskit.ClassicalRegister(3)
        circuit = qiskit.QuantumCircuit(qr, cr)
        circuit.h(qr[0])
        circuit.cx(qr[0], qr[1])
        circuit.snapshot(1)
        circuit.ccx(qr[0], qr[1], qr[2])
        circuit.snapshot(2)
        circuit.reset(qr)
        circuit.snapshot(3)
        circuit.measure(qr, cr)

        sim_cpp = 'local_qasm_simulator_cpp'
        sim_py = 'local_qasm_simulator_py'
        result_cpp = execute(circuit, sim_cpp, shots=2).result()
        result_py = execute(circuit, sim_py, shots=2).result()
        snapshots_cpp = result_cpp.get_snapshots()
        snapshots_py = result_py.get_snapshots()
        self.assertEqual(snapshots_cpp.keys(), snapshots_py.keys())
        snapshot_cpp_1 = result_cpp.get_snapshot(slot='1')
        snapshot_py_1 = result_py.get_snapshot(slot='1')
        self.assertEqual(len(snapshot_cpp_1), len(snapshot_py_1))
        fidelity = state_fidelity(snapshot_cpp_1[0], snapshot_py_1[0])
        self.assertGreater(fidelity, self._desired_fidelity)
コード例 #3
0
    def test_qasm_snapshot(self):
        """snapshot a circuit at multiple places"""
        q = qk.QuantumRegister(3)
        c = qk.ClassicalRegister(3)
        circ = qk.QuantumCircuit(q, c)
        circ.h(q[0])
        circ.cx(q[0], q[1])
        circ.snapshot(1)
        circ.ccx(q[0], q[1], q[2])
        circ.snapshot(2)
        circ.reset(q)
        circ.snapshot(3)

        sim_cpp = 'local_qasm_simulator_cpp'
        sim_py = 'local_qasm_simulator_py'
        result_cpp = execute(circ, sim_cpp, {'shots': 2})
        result_py = execute(circ, sim_py, {'shots': 2})
        snapshots_cpp = result_cpp.get_snapshots()
        snapshots_py = result_py.get_snapshots()
        self.assertEqual(snapshots_cpp.keys(), snapshots_py.keys())
        snapshot_cpp_1 = result_cpp.get_snapshot(slot='1')
        snapshot_py_1 = result_py.get_snapshot(slot='1')
        self.assertEqual(len(snapshot_cpp_1), len(snapshot_py_1))
        fidelity = state_fidelity(snapshot_cpp_1[0], snapshot_py_1[0])
        self.assertGreater(fidelity, self._desired_fidelity)
コード例 #4
0
 def test_sympy(self):
     desired_vector = [
         0,
         math.cos(math.pi / 3) * complex(0, 1) / math.sqrt(4),
         math.sin(math.pi / 3) / math.sqrt(4),
         0,
         0,
         0,
         0,
         0,
         1 / math.sqrt(8) * complex(1, 0),
         1 / math.sqrt(8) * complex(0, 1),
         0,
         0,
         0,
         0,
         1 / math.sqrt(4),
         1 / math.sqrt(4) * complex(0, 1)]
     qr = QuantumRegister(4, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]])
     job = wrapper.execute(qc, 'local_statevector_simulator')
     result = job.result()
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #5
0
 def test_sympy(self):
     desired_vector = [
         0,
         math.cos(math.pi / 3) * complex(0, 1) / math.sqrt(4),
         math.sin(math.pi / 3) / math.sqrt(4),
         0,
         0,
         0,
         0,
         0,
         1 / math.sqrt(8) * complex(1, 0),
         1 / math.sqrt(8) * complex(0, 1),
         0,
         0,
         0,
         0,
         1 / math.sqrt(4),
         1 / math.sqrt(4) * complex(0, 1)]
     qr = QuantumRegister(4, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]])
     job = execute(qc, Aer.get_backend('statevector_simulator'))
     result = job.result()
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #6
0
 def test_random_4qubit(self):
     desired_vector = [
         1 / math.sqrt(4) * complex(0, 1),
         1 / math.sqrt(8) * complex(1, 0),
         0,
         0,
         0,
         0,
         0,
         0,
         1 / math.sqrt(8) * complex(1, 0),
         1 / math.sqrt(8) * complex(0, 1),
         0,
         0,
         0,
         0,
         1 / math.sqrt(4) * complex(1, 0),
         1 / math.sqrt(8) * complex(1, 0)]
     qp = QuantumProgram()
     qr = qp.create_quantum_register("qr", 4)
     cr = qp.create_classical_register("cr", 4)
     qc = qp.create_circuit("qc", [qr], [cr])
     qc.initialize("QInit", desired_vector, [qr[0], qr[1], qr[2], qr[3]])
     result = qp.execute(["qc"], backend='local_qasm_simulator', shots=1)
     quantum_state = result.get_data("qc")['quantum_state']
     fidelity = state_fidelity(quantum_state, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #7
0
    def test_qasm_snapshot(self):
        """snapshot a circuit at multiple places"""
        q = qiskit.QuantumRegister(3)
        c = qiskit.ClassicalRegister(3)
        circ = qiskit.QuantumCircuit(q, c)
        circ.h(q[0])
        circ.cx(q[0], q[1])
        circ.snapshot(1)
        circ.ccx(q[0], q[1], q[2])
        circ.snapshot(2)
        circ.reset(q)
        circ.snapshot(3)

        sim_cpp = 'local_qasm_simulator_cpp'
        sim_py = 'local_qasm_simulator_py'
        result_cpp = execute(circ, sim_cpp, shots=2).result()
        result_py = execute(circ, sim_py, shots=2).result()
        snapshots_cpp = result_cpp.get_snapshots()
        snapshots_py = result_py.get_snapshots()
        self.assertEqual(snapshots_cpp.keys(), snapshots_py.keys())
        snapshot_cpp_1 = result_cpp.get_snapshot(slot='1')
        snapshot_py_1 = result_py.get_snapshot(slot='1')
        self.assertEqual(len(snapshot_cpp_1), len(snapshot_py_1))
        fidelity = state_fidelity(snapshot_cpp_1[0], snapshot_py_1[0])
        self.assertGreater(fidelity, self._desired_fidelity)
コード例 #8
0
    def test_combine_circuit_extension_instructions(self):
        """Test combining circuits contining barrier, initializer, snapshot
        """
        qr = QuantumRegister(2)
        cr = ClassicalRegister(2)
        qc1 = QuantumCircuit(qr)
        desired_vector = [0.5, 0.5, 0.5, 0.5]
        qc1.initialize(desired_vector, qr)
        qc1.barrier()
        qc2 = QuantumCircuit(qr, cr)
        qc2.snapshot(slot='1')
        qc2.measure(qr, cr)
        new_circuit = qc1 + qc2
        backend = 'local_qasm_simulator_py'
        shots = 1024
        result = execute(new_circuit, backend=backend, shots=shots,
                         seed=78).result()

        snapshot_vectors = result.get_snapshot()
        fidelity = state_fidelity(snapshot_vectors[0], desired_vector)
        self.assertGreater(fidelity, 0.99)

        counts = result.get_counts()
        target = {
            '00': shots / 4,
            '01': shots / 4,
            '10': shots / 4,
            '11': shots / 4
        }
        threshold = 0.04 * shots
        self.assertDictAlmostEqual(counts, target, threshold)
コード例 #9
0
 def test_state_fidelity(self):
     psi1 = [0.70710678118654746, 0, 0, 0.70710678118654746]
     psi2 = [0., 0.70710678118654746, 0.70710678118654746, 0.]
     rho1 = [[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]]
     mix = [[0.25, 0, 0, 0], [0, 0.25, 0, 0], [0, 0, 0.25, 0], [0, 0, 0, 0.25]]
     test_pass = round(state_fidelity(psi1, psi1), 7) == 1.0 and \
         round(state_fidelity(psi1, psi2), 8) == 0.0 and \
         round(state_fidelity(psi1, rho1), 8) == 1.0 and \
         round(state_fidelity(psi1, mix), 8) == 0.5 and \
         round(state_fidelity(psi2, rho1), 8) == 0.0 and \
         round(state_fidelity(psi2, mix), 8) == 0.5 and \
         round(state_fidelity(rho1, rho1), 8) == 1.0 and \
         round(state_fidelity(rho1, mix), 8) == 0.5 and \
         round(state_fidelity(mix, mix), 8) == 1.0
     self.assertTrue(test_pass)
コード例 #10
0
ファイル: test_qi.py プロジェクト: theGreenJedi/qiskit-sdk-py
 def test_state_fidelity(self):
     psi1 = [0.70710678118654746, 0, 0, 0.70710678118654746]
     psi2 = [0., 0.70710678118654746, 0.70710678118654746, 0.]
     rho1 = [[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]]
     mix = [[0.25, 0, 0, 0], [0, 0.25, 0, 0],
            [0, 0, 0.25, 0], [0, 0, 0, 0.25]]
     test_pass = round(state_fidelity(psi1, psi1), 7) == 1.0 and \
         round(state_fidelity(psi1, psi2), 8) == 0.0 and \
         round(state_fidelity(psi1, rho1), 8) == 1.0 and \
         round(state_fidelity(psi1, mix), 8) == 0.5 and \
         round(state_fidelity(psi2, rho1), 8) == 0.0 and \
         round(state_fidelity(psi2, mix), 8) == 0.5 and \
         round(state_fidelity(rho1, rho1), 8) == 1.0 and \
         round(state_fidelity(rho1, mix), 8) == 0.5 and \
         round(state_fidelity(mix, mix), 8) == 1.0
     self.assertTrue(test_pass)
コード例 #11
0
 def test_single_qubit(self):
     desired_vector = [1/math.sqrt(3), math.sqrt(2)/math.sqrt(3)]
     qr = QuantumRegister(1, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0]])
     job = wrapper.execute(qc, 'local_statevector_simulator')
     result = job.result()
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #12
0
 def test_deterministic_state(self):
     desired_vector = [0, 1, 0, 0]
     qr = QuantumRegister(2, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1]])
     job = wrapper.execute(qc, 'local_statevector_simulator')
     result = job.result()
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #13
0
 def test_uniform_superposition(self):
     desired_vector = [0.5, 0.5, 0.5, 0.5]
     qr = QuantumRegister(2, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1]])
     result = wrapper.execute(qc,
                              backend_name='local_statevector_simulator')
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #14
0
 def test_deterministic_state(self):
     desired_vector = [0, 1, 0, 0]
     qr = QuantumRegister(2, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1]])
     job = wrapper.execute(qc, 'local_statevector_simulator')
     result = job.result()
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #15
0
 def test_single_qubit(self):
     desired_vector = [1 / math.sqrt(3), math.sqrt(2) / math.sqrt(3)]
     qr = QuantumRegister(1, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0]])
     job = wrapper.execute(qc, 'local_statevector_simulator')
     result = job.result()
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #16
0
 def test_ghz_state(self):
     desired_vector = [1 / math.sqrt(2), 0, 0, 0, 0, 0, 0, 1 / math.sqrt(2)]
     qr = QuantumRegister(3, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1], qr[2]])
     result = wrapper.execute(qc,
                              backend_name='local_statevector_simulator')
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #17
0
 def test_bell_state(self):
     desired_vector = [1/math.sqrt(2), 0, 0, 1/math.sqrt(2)]
     qr = QuantumRegister(2, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1]])
     job = execute(qc, Aer.get_backend('statevector_simulator'))
     result = job.result()
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #18
0
def state_tomography(state, n_qubits, shots):
    # cat target state: [1. 0. 0. ... 0. 0. 1.]/sqrt(2.)
    if state == 'cat':
        target = np.zeros(pow(2, n_qubits))
        target[0] = 1
        target[pow(2, n_qubits)-1] = 1.0
        target /= np.sqrt(2.0)
    # random target state: first column of a random unitary
    elif state == 'random':
        target = random_unitary_matrix(pow(2, n_qubits))[0]
    else:
        raise QISKitError("Unknown state for tomography.")

    print("target: {}".format(target))

    # Use the local qasm simulator
    backend = 'local_qasm_simulator'

    qp = QuantumProgram()

    # Prepared target state and assess quality
    qp = target_prep(qp, state, target)
    prep_result = qp.execute(['prep'], backend='local_statevector_simulator')
    prep_state = prep_result.get_data('prep')['statevector']
    F_prep = state_fidelity(prep_state, target)
    print('Prepared state fidelity =', F_prep)

    # Run state tomography simulation and fit data to reconstruct circuit
    qp, tomo_set, tomo_circuits = add_tomo_circuits(qp)
    tomo_result = qp.execute(tomo_circuits, backend=backend, shots=shots)
    tomo_data = tomo.tomography_data(tomo_result, 'prep', tomo_set)
    rho_fit = tomo.fit_tomography_data(tomo_data)

    # calculate fidelity and purity of fitted state
    F_fit = state_fidelity(rho_fit, target)
    pur = purity(rho_fit)
    print('Fitted state fidelity =', F_fit)
    print('Fitted state purity =', str(pur))

    return qp
コード例 #19
0
def state_tomography(state, n_qubits, shots):
    # cat target state: [1. 0. 0. ... 0. 0. 1.]/sqrt(2.)
    if state == 'cat':
        target = np.zeros(pow(2, n_qubits))
        target[0] = 1
        target[pow(2, n_qubits)-1] = 1.0
        target /= np.sqrt(2.0)
    # random target state: first column of a random unitary
    elif state == 'random':
        target = random_unitary_matrix(pow(2, n_qubits))[0]
    else:
        raise QISKitError("Unknown state for tomography.")

    print("target: {}".format(target))

    # Use the local qasm simulator
    backend = 'local_qasm_simulator'

    qp = QuantumProgram()

    # Prepared target state and assess quality
    qp = target_prep(qp, state, target)
    prep_result = qp.execute(['prep'], backend='local_statevector_simulator')
    prep_state = prep_result.get_data('prep')['statevector']
    F_prep = state_fidelity(prep_state, target)
    print('Prepared state fidelity =', F_prep)

    # Run state tomography simulation and fit data to reconstruct circuit
    qp, tomo_set, tomo_circuits = add_tomo_circuits(qp)
    tomo_result = qp.execute(tomo_circuits, backend=backend, shots=shots)
    tomo_data = tomo.tomography_data(tomo_result, 'prep', tomo_set)
    rho_fit = tomo.fit_tomography_data(tomo_data)

    # calculate fidelity and purity of fitted state
    F_fit = state_fidelity(rho_fit, target)
    pur = purity(rho_fit)
    print('Fitted state fidelity =', F_fit)
    print('Fitted state purity =', str(pur))

    return qp
コード例 #20
0
 def test_single_qubit(self):
     desired_vector = [1 / math.sqrt(3), math.sqrt(2) / math.sqrt(3)]
     qp = QuantumProgram()
     qr = QuantumRegister("qr", 1)
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0]])
     qp.add_circuit("qc", qc)
     result = qp.execute("qc", backend='local_statevector_simulator')
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #21
0
 def test_single_qubit(self):
     desired_vector = [1 / math.sqrt(3), math.sqrt(2) / math.sqrt(3)]
     qp = QuantumProgram()
     qr = qp.create_quantum_register("qr", 1)
     cr = qp.create_classical_register("cr", 1)
     qc = qp.create_circuit("qc", [qr], [cr])
     qc.initialize(desired_vector, [qr[0]])
     result = qp.execute(["qc"], backend='local_qasm_simulator', shots=1)
     quantum_state = result.get_data("qc")['quantum_state']
     fidelity = state_fidelity(quantum_state, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #22
0
 def test_deterministic_state(self):
     desired_vector = [0, 1, 0, 0]
     qp = QuantumProgram()
     qr = qp.create_quantum_register("qr", 2)
     cr = qp.create_classical_register("cr", 2)
     qc = qp.create_circuit("qc", [qr], [cr])
     qc.initialize("QInit", desired_vector, [qr[0], qr[1]])
     result = qp.execute(["qc"], backend='local_qasm_simulator', shots=1)
     quantum_state = result.get_data("qc")['quantum_state']
     fidelity = state_fidelity(quantum_state, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #23
0
 def test_deterministic_state(self):
     desired_vector = [0, 1, 0, 0]
     qp = QuantumProgram()
     qr = QuantumRegister("qr", 2)
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1]])
     qp.add_circuit("qc", qc)
     result = qp.execute("qc", backend='local_statevector_simulator')
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #24
0
 def test_state_fidelity(self):
     psi1 = [0.70710678118654746, 0, 0, 0.70710678118654746]
     psi2 = [0., 0.70710678118654746, 0.70710678118654746, 0.]
     rho1 = [[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]]
     mix = [[0.25, 0, 0, 0], [0, 0.25, 0, 0], [0, 0, 0.25, 0],
            [0, 0, 0, 0.25]]
     self.assertAlmostEqual(state_fidelity(psi1, psi1),
                            1.0,
                            places=7,
                            msg='vector-vector input')
     self.assertAlmostEqual(state_fidelity(psi1, psi2),
                            0.0,
                            places=7,
                            msg='vector-vector input')
     self.assertAlmostEqual(state_fidelity(psi1, rho1),
                            1.0,
                            places=7,
                            msg='vector-matrix input')
     self.assertAlmostEqual(state_fidelity(psi1, mix),
                            0.25,
                            places=7,
                            msg='vector-matrix input')
     self.assertAlmostEqual(state_fidelity(psi2, rho1),
                            0.0,
                            places=7,
                            msg='vector-matrix input')
     self.assertAlmostEqual(state_fidelity(psi2, mix),
                            0.25,
                            places=7,
                            msg='vector-matrix input')
     self.assertAlmostEqual(state_fidelity(rho1, psi1),
                            1.0,
                            places=7,
                            msg='matrix-vector input')
     self.assertAlmostEqual(state_fidelity(rho1, rho1),
                            1.0,
                            places=7,
                            msg='matrix-matrix input')
     self.assertAlmostEqual(state_fidelity(mix, mix),
                            1.0,
                            places=7,
                            msg='matrix-matrix input')
コード例 #25
0
    def test_statevector(self):
        """statevector from a bell state"""
        q = qiskit.QuantumRegister(2)
        circ = qiskit.QuantumCircuit(q)
        circ.h(q[0])
        circ.cx(q[0], q[1])

        sim_cpp = 'local_statevector_simulator_cpp'
        sim_py = 'local_statevector_simulator_py'
        result_cpp = execute(circ, sim_cpp).result()
        result_py = execute(circ, 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))
コード例 #26
0
    def test_combiner(self):
        desired_vector = [0, 1]
        qr = QuantumRegister(1, "qr")
        cr = ClassicalRegister(1, "cr")
        qc1 = QuantumCircuit(qr, cr)
        qc1.initialize([1.0 / math.sqrt(2), 1.0 / math.sqrt(2)], [qr[0]])

        qc2 = QuantumCircuit(qr, cr)
        qc2.initialize([1.0 / math.sqrt(2), -1.0 / math.sqrt(2)], [qr[0]])

        job = wrapper.execute(qc1 + qc2, 'local_statevector_simulator')
        result = job.result()
        quantum_state = result.get_statevector()
        fidelity = state_fidelity(quantum_state, desired_vector)
        self.assertGreater(
            fidelity, self._desired_fidelity,
            "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #27
0
 def test_random_4qubit(self):
     desired_vector = [
         1 / math.sqrt(4) * complex(0, 1), 1 / math.sqrt(8) * complex(1, 0),
         0, 0, 0, 0, 0, 0, 1 / math.sqrt(8) * complex(1, 0),
         1 / math.sqrt(8) * complex(0, 1), 0, 0, 0, 0,
         1 / math.sqrt(4) * complex(1, 0), 1 / math.sqrt(8) * complex(1, 0)
     ]
     qr = QuantumRegister(4, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1], qr[2], qr[3]])
     job = wrapper.execute(qc, 'local_statevector_simulator')
     result = job.result()
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #28
0
def _compare_outputs(data_original, data_compiled, permutation, threshold=ERROR_LIMIT):
    # compare the output states of the original and the compiled circuit
    # the bits of the compiled output state are permuted according to permutation before comparision
    if 'quantum_state' in data_original \
            and 'quantum_state' in data_compiled:
        state = data_compiled.get('quantum_state')
        target = data_original.get('quantum_state')
    elif 'quantum_states' in data_original \
            and 'quantum_states' in data_compiled:
        state = data_compiled.get('quantum_states')[0]
        target = data_original.get('quantum_states')[0]
    else:
        return False
    state = state[_get_perm(permutation)]
    fidelity = state_fidelity(target, state)
    # print("fidelity = ", fidelity)
    return abs(fidelity - 1.0) < threshold
コード例 #29
0
    def test_combiner(self):
        desired_vector = [1, 0]
        qr = QuantumRegister(1, "qr")
        cr = ClassicalRegister(1, "cr")
        qc1 = QuantumCircuit(qr, cr)
        qc1.initialize([1.0 / math.sqrt(2), 1.0 / math.sqrt(2)], [qr[0]])

        qc2 = QuantumCircuit(qr, cr)
        qc2.initialize([1.0 / math.sqrt(2), -1.0 / math.sqrt(2)], [qr[0]])

        job = wrapper.execute(qc1+qc2, 'local_statevector_simulator')
        result = job.result()
        quantum_state = result.get_statevector()
        fidelity = state_fidelity(quantum_state, desired_vector)
        self.assertGreater(
            fidelity, self._desired_fidelity,
            "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #30
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 = 'local_statevector_simulator_cpp'
        sim_py = 'local_statevector_simulator_py'
        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))
コード例 #31
0
ファイル: test_qi.py プロジェクト: christians94/qiskit-sdk-py
 def test_state_fidelity(self):
     psi1 = [0.70710678118654746, 0, 0, 0.70710678118654746]
     psi2 = [0., 0.70710678118654746, 0.70710678118654746, 0.]
     rho1 = [[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]]
     mix = [[0.25, 0, 0, 0], [0, 0.25, 0, 0],
            [0, 0, 0.25, 0], [0, 0, 0, 0.25]]
     self.assertAlmostEqual(state_fidelity(psi1, psi1), 1.0, places=7,
                            msg='vector-vector input')
     self.assertAlmostEqual(state_fidelity(psi1, psi2), 0.0, places=7,
                            msg='vector-vector input')
     self.assertAlmostEqual(state_fidelity(psi1, rho1), 1.0, places=7,
                            msg='vector-matrix input')
     self.assertAlmostEqual(state_fidelity(psi1, mix), 0.25, places=7,
                            msg='vector-matrix input')
     self.assertAlmostEqual(state_fidelity(psi2, rho1), 0.0, places=7,
                            msg='vector-matrix input')
     self.assertAlmostEqual(state_fidelity(psi2, mix), 0.25, places=7,
                            msg='vector-matrix input')
     self.assertAlmostEqual(state_fidelity(rho1, psi1), 1.0, places=7,
                            msg='matrix-vector input')
     self.assertAlmostEqual(state_fidelity(rho1, rho1), 1.0, places=7,
                            msg='matrix-matrix input')
     self.assertAlmostEqual(state_fidelity(mix, mix), 1.0, places=7,
                            msg='matrix-matrix input')
コード例 #32
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 = Aer.get_backend('statevector_simulator')
        result = execute(circuit, sim).result()
        snapshot = result.get_snapshot(slot='3')
        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))
コード例 #33
0
    def test_snapshot(self):
        """snapshot a bell state in the middle of circuit"""
        q = qiskit.QuantumRegister(2)
        c = qiskit.ClassicalRegister(2)
        circ = qiskit.QuantumCircuit(q, c)
        circ.h(q[0])
        circ.cx(q[0], q[1])
        circ.snapshot(3)
        circ.cx(q[0], q[1])
        circ.h(q[1])

        sim = 'local_statevector_simulator_cpp'
        result = execute(circ, sim).result()
        snapshot = result.get_snapshot(slot='3')
        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))
コード例 #34
0
 def test_random_3qubit(self):
     desired_vector = [
         1 / math.sqrt(16) * complex(0, 1),
         1 / math.sqrt(8) * complex(1, 0),
         1 / math.sqrt(16) * complex(1, 1), 0, 0,
         1 / math.sqrt(8) * complex(1, 2),
         1 / math.sqrt(16) * complex(1, 0), 0
     ]
     qp = QuantumProgram()
     qr = QuantumRegister("qr", 3)
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1], qr[2]])
     qp.add_circuit("qc", qc)
     result = qp.execute("qc", backend='local_statevector_simulator')
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #35
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 = Aer.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))
コード例 #36
0
    def test_save_load(self):
        """save |+>|0>, do some stuff, then load"""
        q = qiskit.QuantumRegister(2)
        c = qiskit.ClassicalRegister(2)
        circ = qiskit.QuantumCircuit(q, c)
        circ.h(q[0])
        circ.save(1)
        circ.cx(q[0], q[1])
        circ.cx(q[1], q[0])
        circ.h(q[1])
        circ.load(1)

        sim = 'local_statevector_simulator_cpp'
        result = execute(circ, 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))
コード例 #37
0
    def run(self):
        evo_time = 1
        # get the groundtruth via simple matrix * vector
        state_out_exact = self._operator.evolve(self._initial_state.construct_circuit('vector'), evo_time, 'matrix', 0)

        qr = QuantumRegister(self._operator.num_qubits, name='q')
        circuit = self._initial_state.construct_circuit('circuit', qr)
        circuit += self._operator.evolve(
            None, evo_time, 'circuit', 1,
            quantum_registers=qr,
            expansion_mode='suzuki',
            expansion_order=self._expansion_order
        )

        result = self.execute(circuit)
        state_out_dynamics = np.asarray(result.get_statevector(circuit))

        self._ret['score'] = state_fidelity(state_out_exact, state_out_dynamics)

        return self._ret
コード例 #38
0
 def test_random_3qubit(self):
     desired_vector = [
         1 / math.sqrt(16) * complex(0, 1),
         1 / math.sqrt(8) * complex(1, 0),
         1 / math.sqrt(16) * complex(1, 1),
         0,
         0,
         1 / math.sqrt(8) * complex(1, 2),
         1 / math.sqrt(16) * complex(1, 0),
         0]
     qr = QuantumRegister(3, "qr")
     qc = QuantumCircuit(qr)
     qc.initialize(desired_vector, [qr[0], qr[1], qr[2]])
     job = wrapper.execute(qc, 'local_statevector_simulator')
     result = job.result()
     statevector = result.get_statevector()
     fidelity = state_fidelity(statevector, desired_vector)
     self.assertGreater(
         fidelity, self._desired_fidelity,
         "Initializer has low fidelity {0:.2g}.".format(fidelity))
コード例 #39
0
 def compare_statevector(self,
                         result,
                         circuits,
                         targets,
                         global_phase=True,
                         places=None):
     """Compare final statevectors to targets."""
     for pos, test_case in enumerate(zip(circuits, targets)):
         circuit, target = test_case
         output = result.get_statevector(circuit)
         msg = ("Circuit ({}/{}):".format(pos + 1, len(circuits)) +
                " {} != {}".format(output, target))
         if (global_phase):
             # Test equal including global phase
             self.assertAlmostEqual(norm(output - target),
                                    0,
                                    places=places,
                                    msg=msg)
         else:
             # Test equal ignorning global phase
             self.assertAlmostEqual(state_fidelity(output, target) - 1,
                                    0,
                                    places=places,
                                    msg=msg + " up to global phase")
コード例 #40
0
    for j in range(1, i + 1):
        for k in range(1, j):
            qc2.rz(-b[j], q[0])
            qc2.rx(-a[j], q[0])
        qc2.rx(a[j], q[0])
        qc2.rz(b[j], q[0])
        for k in range(1, j):
            qc2.rx(a[j], q[0])
            qc2.rz(b[j], q[0])

    backend = Aer.get_backend('statevector_simulator')
    job = execute(qc2, backend)
    qc2_state = job.result().get_statevector(qc2)
    qc2_state
    fidelity = state_fidelity(env, qc2_state)
    print('Fidelity = ' + str(state_fidelity(env, qc2_state)))
    #if((fidelity>=0.9)&(ofidelity <= 0.9)):
    #    delta = pi/8
    #print('Delta = ' + str(delta))
    ofidelity = fidelity
    #circuit_drawer(qc2)

q = QuantumRegister(1, 'q')
qc = QuantumCircuit(q)

for j in range(1, cycle + 1):
    for k in range(1, j):
        qc.rz(-b[j], q[0])
        qc.rx(-a[j], q[0])
    qc.rx(a[j], q[0])