pyqrack.qrack_neuron

Classes

QrackNeuron

Class that exposes the QNeuron class of Qrack

Module Contents

class pyqrack.qrack_neuron.QrackNeuron(simulator, controls, target, activation_fn=NeuronActivationFn.Sigmoid, alpha=1.0, _init=True, _isTorch=False)

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

activation_fn

Activation function choice

Type:

NeuronActivationFn

alpha

Activation function parameter, if required

Type:

float

angles

(or c_double) Memory for neuron prediction angles

Type:

list[ctypes.c_float]

_get_error()
_throw_if_error()
simulator
controls
target
activation_fn
alpha = 1.0
angles = None
nid
__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.

static _ulonglong_byref(a)
static _real1_byref(a)
set_simulator(s, controls=None, target=None)

Set the neuron simulator

Set the simulator used by this neuron

Parameters:
  • s (QrackSimulator) – The simulator to use

  • controls (list[int]) – The control qubit IDs to use

  • target (int) – The output qubit ID to use

Raises:

RuntimeError – QrackSimulator raised an exception.

set_qubit_ids(controls, target=None)

Set the neuron qubit identifiers

Set the control and target qubits within the simulator

Parameters:
  • controls (list[int]) – The control qubit IDs to use

  • target (int) – The output qubit ID to use

Raises:

RuntimeError – QrackSimulator raised an exception.

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).

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.

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.

static quantile_bounds(vec, bits)

Calculate vector quantile bounds

This is a static helper method to calculate the quantile bounds of 2 ** bits worth of quantiles.

Parameters:
  • vec – numerical vector

  • bits – log2() of quantile count

Returns:

Quantile (n + 1) bounds for n-quantile division, including minimum and maximum values

static discretize(vec, bounds)

Discretize vector by quantile bounds

This is a static helper method to discretize a numerical vector according to quantile bounds calculated by the quantile_bounds(vec, bits) static method.

Parameters:
  • vec – numerical vector

  • bounds – (n + 1) n-quantile bounds including extrema

Returns:

Discretized bit-row vector, least-significant first

static flatten_and_transpose(arr)

Flatten and transpose feature matrix

This is a static helper method to convert a multi-feature bit-row matrix to an observation-row matrix with flat feature columns.

Parameters:

arr – bit-row matrix

Returns:

Observation-row matrix with flat feature columns

static bin_endpoints_average(bounds)

Bin endpoints average

This is a static helper method that accepts the output bins from quantile_bounds() and returns the average points between the bin endpoints. (This is NOT always necessarily the best heuristic for how to convert binned results back to numerical results, but it is often a reasonable way.)

Parameters:

bounds – (n + 1) n-quantile bounds including extrema

Returns:

List of average points between the bin endpoints