Exemple #1
0
def main():
    # GENERAL SETTINGS
    num_system_qubits = 3
    num_ancillae = 1
    num_qubits = num_system_qubits + num_ancillae
    n_attempts = 20
    logging.info('We are going for 20 attempts.')
    # SET TARGET GATE AND INTERACTIONS
    target_gate = qutip.fredkin()
    # interactions = 'all'
    fredkin_model = make_XX_model(num_qubits)
    # HYPERPARAMETERS
    training_dataset_size = 200
    test_dataset_size = 100
    n_epochs = 300
    batch_size = 2
    sgd_method = 'adadelta'
    learning_rate = 1
    decay_rate = 0.1
    initial_values = 'random'
    logging.info('Random initial values')
    # TAKE CARE NOT TO OVERWRITE PREVIOUSLY SAVED FILES
    prefix = os.path.join(OUTPUT_DIR, OUTPUT_FILES_NAME)
    ext = '.pickle'
    files = glob.glob(prefix + '*')
    pre_idx = 0
    if files:
        pre_idx = max([int(f[len(prefix):-len(ext)]) for f in files])
    # STARTING MESSAGES
    for idx in range(n_attempts):
        logging.info('Starting training no.{}'.format(str(idx + 1)))

        model = qn.model.QubitNetworkGateModel(
            sympy_expr=fredkin_model,
            initial_values=initial_values,
            num_system_qubits=num_system_qubits)
        optimizer = qn.Optimizer.Optimizer(
            net=model,
            learning_rate=learning_rate,
            decay_rate=decay_rate,
            n_epochs=n_epochs,
            batch_size=batch_size,
            target_gate=target_gate,
            training_dataset_size=training_dataset_size,
            test_dataset_size=test_dataset_size,
            sgd_method=sgd_method,
            headless=True)

        optimizer.run()

        newpath = os.path.join(
            OUTPUT_DIR, OUTPUT_FILES_NAME + str(1 + pre_idx + idx) + '.pickle')

        optimizer.save_results(newpath)
        logging.info('Fidelity obtained: {}'.format(model.average_fidelity()))
def main():
    # GENERAL SETTINGS
    num_system_qubits = 3
    num_ancillae = 0
    num_qubits = num_system_qubits + num_ancillae
    # the attempts will be equally (when possible) distributed among these
    # initial values. Each initial value will be used the same amount of
    # times.
    attempted_initvalues = [0, 1, 2, 3, 4]
    num_attempts_per_initvalue = 10
    num_attempts = num_attempts_per_initvalue * len(attempted_initvalues)
    logging.info('We are going for a total of {}'
                 'attempts.'.format(num_attempts))
    logging.info('The following initial values will be used:'
                 '{}.'.format(attempted_initvalues))
    # SET TARGET GATE AND INTERACTIONS
    target_gate = qutip.fredkin()
    # interactions = 'all'
    # fredkin_model = make_reduced_fredkin_model(num_qubits)
    # HYPERPARAMETERS
    training_dataset_size = 200
    test_dataset_size = 100
    n_epochs = 100
    batch_size = 2
    sgd_method = 'momentum'
    learning_rate = 1
    decay_rate = 0.1
    # TAKE CARE NOT TO OVERWRITE PREVIOUSLY SAVED FILES
    prefix = os.path.join(OUTPUT_DIR, OUTPUT_FILES_NAME)
    ext = '.pickle'
    files = glob.glob(prefix + '*')
    pre_idx = 0
    if files:
        pre_idx = max([int(f[len(prefix):-len(ext)]) for f in files])
    # STARTING MESSAGES
    for idx in range(num_attempts):
        initial_values = attempted_initvalues[idx //
                                              num_attempts_per_initvalue]
        fredkin_model = make_reduced_fredkin_model(num_qubits)
        logging.info('Starting training no.{}'.format(str(idx + 1)))
        logging.info('Initial values: {}.'.format(initial_values))

        model = qn.model.QubitNetworkGateModel(
            sympy_expr=fredkin_model,
            initial_values=initial_values,
            num_system_qubits=num_system_qubits)
        optimizer = qn.Optimizer.Optimizer(
            net=model,
            learning_rate=learning_rate,
            decay_rate=decay_rate,
            n_epochs=n_epochs,
            batch_size=batch_size,
            target_gate=target_gate,
            training_dataset_size=training_dataset_size,
            test_dataset_size=test_dataset_size,
            sgd_method=sgd_method,
            headless=True)

        optimizer.run()

        newpath = os.path.join(
            OUTPUT_DIR, OUTPUT_FILES_NAME + str(1 + pre_idx + idx) + '.pickle')

        optimizer.save_results(newpath)
        logging.info('Fidelity obtained: {}'.format(model.average_fidelity()))