Exemplo n.º 1
0
if __name__ == "__main__":
    # update hyperparameters with command line arguments
    HP = parse_arguments(HP)

    # set the target state
    target_state = HP['target_state_fn'](cutoff=HP['cutoff'], **HP['state_params'])
    HP['modes'] = int(np.log(target_state.shape[0])/np.log(HP['cutoff']))

    print('------------------------------------------------------------------------')
    print('Hyperparameters:')
    print('------------------------------------------------------------------------')
    for key, val in HP.items():
        print("{}: {}".format(key, val))
    print('------------------------------------------------------------------------')

    # calculate the learnt state and return the gate parameters
    print('Constructing variational quantum circuit...')
    ket, parameters = variational_quantum_circuit(**HP)

    # flatten ket to take into account two mode states
    ket = tf.reshape(ket, [-1])

    # perform the optimization
    print('Beginning optimization...')
    res = optimize(ket, target_state, parameters, **HP)

    # save plots
    print('Generating plots...')
    save_plots(res['learnt_state'], target_state, res['cost_progress'], **HP)
Exemplo n.º 2
0
    )
    for key, val in HP.items():
        print("{}: {}".format(key, val))
    print(
        '------------------------------------------------------------------------'
    )

    # produce a batch_size array of one mode initial Fock states
    in_ket = np.zeros([HP['gate_cutoff'], HP['cutoff']])
    np.fill_diagonal(in_ket, 1)

    if HP['modes'] == 2:
        # take the outer product of the one mode input states
        in_ket = np.einsum('ij,kl->ikjl', in_ket, in_ket)
        # reshape to be the correct shape for Strawberry Fields
        in_ket = in_ket.reshape(HP['gate_cutoff']**2, HP['cutoff'],
                                HP['cutoff'])

    # calculate the learnt state and return the gate parameters
    print('Constructing variational quantum circuit...')
    ket, parameters = variational_quantum_circuit(input_state=in_ket, **HP)

    # perform the optimization
    print('Beginning optimization...')
    res = optimize(ket, target_unitary, parameters, **HP)

    # save plots
    print('Generating plots...')
    save_plots(target_unitary, res['learnt_unitary'], res['eq_state_learnt'],
               res['eq_state_target'], res['cost_progress'], **HP)