pyqrack.qrack_neuron

Module Contents

Classes

QrackNeuron

Class that exposes the QNeuron class of Qrack

class pyqrack.qrack_neuron.QrackNeuron(simulator, controls, target, activation_fn=NeuronActivationFn.Sigmoid, alpha=1.0, tolerance=sys.float_info.epsilon, _init=True)

Class that exposes the QNeuron class of Qrack

This model of a “quantum neuron” is based on the concept of a “uniformly controlled” rotation of a single output qubit around the Pauli Y axis, and has been developed by others. In our case, the primary relevant gate could also be called a single-qubit-target multiplexer.

(See https://arxiv.org/abs/quant-ph/0407010 for an introduction to “uniformly controlled gates.)

QrackNeuron is meant to be interchangeable with a single classical neuron, as in conventional neural net software. It differs from classical neurons in conventional neural nets, in that the “synaptic cleft” is modelled as a single qubit. Hence, this neuron can train and predict in superposition.

nid

Qrack ID of this neuron

Type:

int

simulator

Simulator instance for all synaptic clefts of the neuron

Type:

QrackSimulator

controls

Indices of all “control” qubits, for neuron input

Type:

list(int)

target

Index of “target” qubit, for neuron output

Type:

int

tolerance

Rounding tolerance

Type:

double

_get_error()
_throw_if_error()
__del__()
clone()

Clones this neuron.

Create a new, independent neuron instance with identical angles, inputs, output, and tolerance, for the same QrackSimulator.

Raises:

RuntimeError – QrackNeuron C++ library raised an exception.

_ulonglong_byref(a)
_real1_byref(a)
set_angles(a)

Directly sets the neuron parameters.

Set all synaptic parameters of the neuron directly, by a list enumerated over the integer permutations of input qubits.

Parameters:

a (list(double)) – List of input permutation angles

Raises:
  • ValueError – Angles ‘a’ in QrackNeuron.set_angles() must contain at least (2 ** len(self.controls)) elements.

  • RuntimeError – QrackSimulator raised an exception.

get_angles()

Directly gets the neuron parameters.

Get all synaptic parameters of the neuron directly, as a list enumerated over the integer permutations of input qubits.

Raises:

RuntimeError – QrackNeuron C++ library raised an exception.

set_alpha(a)

Set the neuron ‘alpha’ parameter.

To enable nonlinear activation, QrackNeuron has an ‘alpha’ parameter that is applied as a power to its angles, before learning and prediction. This makes the activation function sharper (or less sharp).

Raises:

RuntimeError – QrackNeuron C++ library raised an exception.

set_activation_fn(f)

Sets the activation function of this QrackNeuron

Nonlinear activation functions can be important to neural net applications, like DNN. The available activation functions are enumerated in NeuronActivationFn.

Raises:

RuntimeError – QrackNeuron C++ library raised an exception.

predict(e=True, r=True)

Predict based on training

“Predict” the anticipated output, based on input and training. By default, “predict()” will initialize the output qubit as by resetting to |0> and then acting a Hadamard gate. From that state, the method amends the output qubit upon the basis of the state of its input qubits, applying a rotation around Pauli Y axis according to the angle learned for the input.

Parameters:
  • e (bool) – If False, predict the opposite

  • r (bool) – If True, start by resetting the output to 50/50

Raises:

RuntimeError – QrackNeuron C++ library raised an exception.

unpredict(e=True)

Uncompute a prediction

Uncompute a ‘prediction’ of the anticipated output, based on input and training.

Parameters:

e (bool) – If False, unpredict the opposite

Raises:

RuntimeError – QrackNeuron C++ library raised an exception.

learn_cycle(e=True)

Run a learning cycle

A learning cycle consists of predicting a result, saving the classical outcome, and uncomputing the prediction.

Parameters:

e (bool) – If False, predict the opposite

Raises:

RuntimeError – QrackNeuron C++ library raised an exception.

learn(eta, e=True, r=True)

Learn from current qubit state

“Learn” to associate current inputs with output. Based on input qubit states and volatility ‘eta,’ the input state synaptic parameter is updated to prefer the “e” (“expected”) output.

Parameters:
  • eta (double) – Training volatility, 0 to 1

  • e (bool) – If False, predict the opposite

  • r (bool) – If True, start by resetting the output to 50/50

Raises:

RuntimeError – QrackNeuron C++ library raised an exception.

learn_permutation(eta, e=True, r=True)

Learn from current classical state

Learn to associate current inputs with output, under the assumption that the inputs and outputs are “classical.” Based on input qubit states and volatility ‘eta,’ the input state angle is updated to prefer the “e” (“expected”) output.

Parameters:
  • eta (double) – Training volatility, 0 to 1

  • e (bool) – If False, predict the opposite

  • r (bool) – If True, start by resetting the output to 50/50

Raises:

RuntimeError – QrackNeuron C++ library raised an exception.