Пример #1
0
def random_unitary_matrix(dim, seed=None):
    """
    Return a random unitary ndarray over num qubits.

    Args:
        dim (int): the dim of the state space.
        seed (int): Optional. To set a random seed.
    Returns:
        ndarray: U (2**num, 2**num) unitary ndarray.
    """
    unitary = np.zeros([dim, dim], dtype=complex)
    for j in range(dim):
        if j == 0:
            a = random_state(dim, seed)
        else:
            a = random_state(dim)
        unitary[:, j] = np.copy(a)
        # Grahm-Schmidt Orthogonalize
        i = j - 1
        while i >= 0:
            dc = np.vdot(unitary[:, i], a)
            unitary[:, j] = unitary[:, j] - dc * unitary[:, i]
            i = i - 1
        # normalize
        unitary[:, j] = unitary[:, j] * (
            1.0 / np.sqrt(np.vdot(unitary[:, j], unitary[:, j])))
    return unitary
Пример #2
0
 def test_random_state_circuit(self):
     state = random_state(3)
     q = QuantumRegister(3)
     qc = QuantumCircuit(q)
     qc.initialize(state, [q[0], q[1], q[2]])
     backend = Aer.get_backend('statevector_simulator_py')
     qc_state = execute(qc, backend).result().get_statevector(qc)
     self.assertAlmostEqual(state_fidelity(qc_state, state), 1.0, places=7)
Пример #3
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)
Пример #4
0
 def test_random_state_circuit(self):
     """TO BE REMOVED Run initizalized circuit"""
     with self.assertWarns(DeprecationWarning):
         state = random_state(2**3, seed=40)
     # Initializer test should be elsewhere
     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)
Пример #5
0
 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)
Пример #6
0
def main():

    print("Loading Qiskit Packages...")

    # getting usser input
    display_report = input("Display the full report at the end y/n? ")
    output_file = input("Name your output file (.csv): ")

    layers = int(input("input an integer number of layers: "))

    # Initializing required parameters for call of minimize
    backend = Aer.get_backend('statevector_simulator')
    random_state = quantum_info.random_state(16, seed=101)
    process_stats = optimization_stats()
    angle_bounds = [(0, 2 * np.pi)] * layers * 8
    qr = QuantumRegister(4)
    theta = init_theta(layers)

    print("Optimization Starts (method: L-BFGS-B): ")

    # call for minimize on quantum_simulator() see above for specifications
    result = minimize(quantum_simulator,
                      theta,
                      method='L-BFGS-B',
                      bounds=angle_bounds,
                      args=(random_state, layers, backend, qr, process_stats),
                      options={
                          'ftol': 2.22e-08,
                          'gtol': 1e-8,
                          'maxiter': 100000
                      })
    optimization_data = process_stats.create_data_frame(layers)

    # print report based on user input
    if display_report.upper() != 'Y':
        print("optimization success: ", bool(result.success))
    else:
        print('\n\n', result)
        print(result.x)
        print("epsilon :", result.fun)
        print(optimization_data)

    plot_process(optimization_data)
    optimization_data.to_csv(output_file)