def main():
    # We add the regularizer function to the model
    # The strength of regularizer is regulated by the
    # hyperparameter 'regularization_strength'.
    # Setting 'plot' to an integer automatically plots some default values
    # as well as the monitored circuit parameters. (Requires matplotlib).
    hyperparams = {
        'circuit': circuit_with_cost_function,
        'init_circuit_params': my_init_params,
        'task': 'optimization',
        'loss': myloss,
        'regularizer': myregularizer,
        'regularization_strength': 0.5,
        'optimizer': 'SGD',
        # 'init_learning_rate': 1e-7,
        'init_learning_rate': 1e-1,
        'log_every': 1,
        'plot': True
    }

    learner = CircuitLearner(hyperparams=hyperparams)

    learner.train_circuit(steps=50)

    # Print out the final parameters
    final_params = learner.get_circuit_parameters()
    # final_params is a dictionary
    for name, value in final_params.items():
        print("Parameter {} has the final value {}.".format(name, value))

    n_qmodes = 4
    all_results = []

    final_params_translated = []
    final_params_translated.append(final_params["regularized/squeeze_0"])
    final_params_translated.append(final_params["regularized/squeeze_1"])
    final_params_translated.append(final_params["regularized/squeeze_2"])
    final_params_translated.append(final_params["regularized/squeeze_3"])
    final_params_translated.append(final_params["regularized/displacement_0"])
    final_params_translated.append(final_params["regularized/displacement_1"])
    final_params_translated.append(final_params["regularized/displacement_2"])
    final_params_translated.append(final_params["regularized/displacement_3"])
    final_params_translated.append(final_params["regularized/bs_00"])
    final_params_translated.append(final_params["regularized/bs_01"])
    final_params_translated.append(final_params["regularized/bs_10"])
    final_params_translated.append(final_params["regularized/bs_11"])
    final_params_translated.append(final_params["regularized/bs_20"])
    final_params_translated.append(final_params["regularized/bs_21"])
    final_params_translated.append(final_params["regularized/bs_30"])
    final_params_translated.append(final_params["regularized/bs_31"])

    for i in range(1):
        bits = get_bits_from_circuit(A, n_qmodes, final_params_translated)
        string_bits = [str(bit) for bit in bits]
        all_results.append(",".join(string_bits))

    print(Counter(all_results))
Exemplo n.º 2
0
 def get_circuit_params(self, steps):
     learner = CircuitLearnerNUM(hyperparams=self.hyperp)
     learner.train_circuit(X=self.X, Y=self.Y, steps=steps)
     params = learner.get_circuit_parameters()
     param_value = params['regularized/dummy']
     return param_value
Exemplo n.º 3
0
# We add the regularizer function to the model
# The strength of regularizer is regulated by the
# hyperparameter 'regularization_strength'.
# Setting 'plot' to an integer automatically plots some default values
# as well as the monitored circuit parameters. (Requires matplotlib).
hyperparams = {
    'circuit': circuit,
    'init_circuit_params': my_init_params,
    'task': 'optimization',
    'loss': myloss,
    'regularizer': myregularizer,
    'regularization_strength': 0.5,
    'optimizer': 'SGD',
    'init_learning_rate': 0.1,
    'log_every': 1,
    'plot': True
}

learner = CircuitLearner(hyperparams=hyperparams)

learner.train_circuit(steps=50)

# Print out the final parameters
final_params = learner.get_circuit_parameters()
# final_params is a dictionary
for name, value in final_params.items():
    print("Parameter {} has the final value {}.".format(name, value))

# Look in the 'logsNUM' directory, there should be a file called 'log.csv' that records what happened to alpha
# during training. Play around with the 'regularization_strength' and see how a large strength forces alpha to zero.