LLY-DML is part of the LILY project and is a Quantum Machine Learning model. It uses so-called L-Gates. These gates are Machine Learning gates that modify their state based on an input to map to a desired state of an input.
The Circuit class is used to create, manage, and validate quantum circuits based on input data from JSON files. Additionally, it allows for performing measurements at the end of the circuit.
Class Attributes
data_path (str): Path to the data.json file, which contains basic information such as the number of qubits and the depth of the circuit.
train_path (str): Path to the train.json file, which contains activation and training matrices for the circuit.
qubits (int): Number of qubits in the circuit, extracted from data.json.
depth (int): Depth of the circuit, i.e., how many L-gates per qubit are present, extracted from data.json.
activation_matrices (dict): Contains named activation matrices loaded from train.json.
training_matrices (dict): Contains named training matrices loaded from train.json.
input_data (list): List of dictionaries containing converted input data based on the activation and training matrices.
circuit (dict): Structure for storing the entire circuit, organized by qubits and their respective L-gates.
measurements (list): List of measurement results after performing measurements.
Verifies the consistency of a given activation matrix in terms of the number of rows, columns, and data types.
Parameters:
matrix (list): The matrix to be checked.
Functionality:
Checks if the matrix is a list.
Verifies if the number of rows matches the number of qubits.
For each row:
Checks if the row is a list.
Checks if the row has the expected number of columns (depth * 3).
Ensures all elements are numeric values (int or float).
Return:
True if the matrix is consistent, otherwise False.
5. _is_empty_matrix(self, matrix)
Description:
Checks if the given matrix consists solely of zeros.
Parameters:
matrix (list): The matrix to be checked.
Functionality:
Iterates over each row and element.
Returns False if any element is non-zero.
Returns True if all elements are zero.
Return:
True if all elements are zero, otherwise False.
6. check_input_data(self)
Description:
Verifies the structure and consistency of input_data.
Parameters:
None.
Functionality:
Checks if input_data is a list.
Verifies that the number of entries is equal to qubits * depth.
Checks each entry dictionary:
Contains all required keys (Row, Position, ActivationTriple, TrainingTriple).
Values are of the expected types (int for Row and Position, list for ActivationTriple and TrainingTriple).
ActivationTriple and TrainingTriple have exactly three elements.
Verifies that the number of entries per row is equal to depth.
Returns True if all checks pass, otherwise False.
Return:
True if input_data is consistent, otherwise False.
7. create_L_gate(self, entry)
Description:
This method creates the parameters for a single L-gate within the quantum circuit based on an entry from input_data. It processes the activation and training data, creates a circuit blueprint for the L-gate, and manages the incrementation of the current position and row. Throughout the process, success codes and error codes are returned to monitor execution status.
Parameters:
entry (dict): A dictionary from input_data with the keys:
Row: Specifies the row where the 3-part pair is located.
Position: Specifies which 3-part pair it is in the row.
ActivationTriple: 3-part pair from the activation matrix.
TrainingTriple: 3-part pair from the training matrix.
Functionality:
Entry Initialization and Validation:
a. Entry Validation:
Description: Ensures entry is a valid dictionary containing all required keys (Row, Position, ActivationTriple, TrainingTriple).
Success Code:2060 – Entry successfully validated.
Error Code:1060 – Invalid entry or missing keys.
b. List Length Validation:
Description: Checks if both ActivationTriple and TrainingTriple are lists with exactly three elements.
Success Code:2061 – List lengths are correct.
Error Code:1061 – Invalid list lengths in ActivationTriple or TrainingTriple.
L-Gate Circuit Creation:
a. Data Extraction:
Description:
Retrieves the relevant segment from input_data based on the current Row and Position.
Extracts data from the activation matrix (ActivationTriple) and the training matrix (TrainingTriple).
Success Code:2062 – Data successfully extracted.
Error Code:1062 – Error in data extraction.
b. Data Merging:
Description:
Combines activation and training values to create parameters for phase gates.
Creates a circuit blueprint with the following structure:
Row:1 – The L-gate is applied to the qubit in the second row (index 1).
End:true – This indicates that this is the last operation in the current sequence of the L-gate.
Gates: A list of applied gates with their respective parameters (P(value), H).
Failed:
Error code (1060 to 1065) and a corresponding error message describing the reason for the failure.
Example:
{
"ErrorCode": 1061,
"ErrorMessage": "Invalid list lengths in 'ActivationTriple' or 'TrainingTriple'."
}
ErrorCode:1061 – Invalid list lengths in ActivationTriple or TrainingTriple.
ErrorMessage: Clear description of the issue to quickly identify the cause.
Intermediate Factors and Codes:
Code
Description
2060
Entry successfully validated.
2061
Activation and training data list lengths are correct.
2062
Data successfully extracted.
2063
Circuit blueprint successfully created.
2064
Position successfully incremented.
2065
Row successfully incremented and switched to the next row.
1060
Invalid entry or missing keys.
1061
Invalid list lengths in ActivationTriple or TrainingTriple.
1062
Error in data extraction.
1063
Error in merging data or creating the blueprint.
1064
Position exceeds the number of available 3-part pairs.
1065
No more rows available in input_data (end of data structure).
Additional Notes:
Data structure of input_data:
Row: Specifies the row where the 3-part pair is located.
Position: Specifies which 3-part pair it is in the row.
ActivationTriple: A 3-part pair from the activation matrix.
TrainingTriple: A 3-part pair from the training matrix.
Error Handling:
The method should ensure that when an error occurs, the corresponding error code is returned along with a clear error message to quickly identify and fix the issue.
Status Monitoring:
The use of success codes and error codes allows for precise monitoring and logging of the method’s state during execution, which is especially helpful for error diagnosis and debugging.
Further Processing:
After successful creation and incrementation, the generated circuit blueprint can be integrated into the main circuit (self.circuit) to form the complete quantum circuit.
Sample Execution:
Successful L-Gate Creation
Input Data (entry):
entry = {
"Row": 1,
"Position": 0,
"ActivationTriple": [10, 20, 30],
"TrainingTriple": [40, 50, 60]
}
**Sample Execution:**
### Successful L-Gate Creation
**Input Data (`entry`):**
```python
entry = {
"Row": 1,
"Position": 0,
"ActivationTriple": [10, 20, 30],
"TrainingTriple": [40, 50, 60]
}
Return Value:
json
Code kopieren
{
"Row": 1,
"End": true,
"Gates": [
"P(10)",
"P(40)",
"H",
"P(20)",
"P(50)",
"H",
"P(30)",
"P(60)"
]
}
Failed L-Gate Creation
Input Data (entry):
python
Code kopieren
entry = {
"Row": 0,
"Position": 1,
"ActivationTriple": [5, 15], # Missing element
"TrainingTriple": [25, 35, 45]
}
Return Value:
json
Code kopieren
{
"ErrorCode": 1061,
"ErrorMessage": "Invalid list lengths in 'ActivationTriple' or 'TrainingTriple'."
}
### 8. `create_initial_circuit(self)`
**Description:**
This method creates the initial circuit (`circuit`). The following steps are carried out:
1. **Pass Matrix Name:**
- Pass the matrix name.
2. **Read Data:**
- Call the `read_data` method.
3. **Check Consistency:**
- Check the consistency of the activation matrix.
4. **Convert Data:**
- Convert data using the `convert_input_data` method.
5. **Check Input Data:**
- Check `input_data` using the `check_input_data` method.
6. **Create Circuit:**
- Initialize the `circuit` with the number of qubits.
- Loop to create L-gates:
- Use the `create_L_gate` method, which returns the subcircuit.
- Append the subcircuit to the corresponding qubit in the `circuit`.
- Continue until the row flag (`End`) is reached.
- Switch to the next qubit and start circuit realization.
7. **Check Circuit:**
- Call the `check_circuit` method, which checks the following parameters:
- Number of qubits matches the value `qubits`.
- Each qubit has `6 * depth` phase gates.
- Number of Hadamard gates matches `3 * depth`.
**Error Handling:**
- Error codes `1066` to `1069` for specific errors during circuit creation.
---
### `check_circuit(self)`
**Description:**
This method verifies the created circuit (`circuit`) for consistency in terms of the number of qubits, phase gates, and Hadamard gates.
**Parameters:**
- None.
**Functionality:**
- Verifies that the number of qubits in the `circuit` matches the `qubits` attribute.
- For each qubit:
- Verifies the number of phase gates (`P` gates) is equal to `6 * depth`.
- Verifies the number of Hadamard gates (`H`) is equal to `3 * depth`.
**Return:**
- `True` if the circuit is consistent, otherwise `False`.
**Error Handling:**
- Error code `1066`: Incorrect number of qubits in the circuit.
- Error code `1067`: Incorrect number of phase gates on a qubit.
- Error code `1068`: Incorrect number of Hadamard gates on a qubit.
---
### `measure(self, shots)`
**Description:**
This method performs measurements on the circuit.
**Parameters:**
- `shots` (`int`): Specifies how many times the circuit is executed.
**Functionality:**
- Executes the circuit `shots` times.
- Stores the measurement results in the `measurements` list.
**Return:**
- Returns the measurement results.
**Success Code:**
- `2066` – Measurements successfully performed.
**Error Code:**
- `1069` – Error during measurements.
---
## Method Overview
Here is a summary of all methods in the `Circuit` class with their associated success and error codes:
| Method | Description | Codes |
|-------------------------|----------------------------------------------------------|-------------------------|
| `__init__` | Initializes a new instance of the `Circuit` class. | - |
| `read_data` | Reads and extracts data from `data.json` and `train.json`. | 2060, 2061 / 1060, 1061 |
| `convert_input_data` | Converts input data based on the options. | 2064, 2063 / 1061, 1063, 1064 |
| `_is_matrix_consistent` | Checks the consistency of an activation matrix. | - |
| `_is_empty_matrix` | Checks if a matrix consists solely of zeros. | - |
| `check_input_data` | Checks the structure and consistency of `input_data`. | - |
| `create_L_gate` | Creates a circuit blueprint for an L-gate based on an entry. | 2060-2065 / 1060-1065 |
| `create_initial_circuit` | Creates the initial circuit based on the converted data. | 2066 / 1066-1069 |
| `check_circuit` | Checks the consistency of the created circuit. | 1066-1068 |
| `measure` | Performs measurements on the circuit and returns the results. | 2066 / 1069 |
The
Circuit
class is used to create, manage, and validate quantum circuits based on input data from JSON files. Additionally, it allows for performing measurements at the end of the circuit.Class Attributes
data_path
(str
): Path to thedata.json
file, which contains basic information such as the number of qubits and the depth of the circuit.train_path
(str
): Path to thetrain.json
file, which contains activation and training matrices for the circuit.qubits
(int
): Number of qubits in the circuit, extracted fromdata.json
.depth
(int
): Depth of the circuit, i.e., how many L-gates per qubit are present, extracted fromdata.json
.activation_matrices
(dict
): Contains named activation matrices loaded fromtrain.json
.training_matrices
(dict
): Contains named training matrices loaded fromtrain.json
.input_data
(list
): List of dictionaries containing converted input data based on the activation and training matrices.circuit
(dict
): Structure for storing the entire circuit, organized by qubits and their respective L-gates.measurements
(list
): List of measurement results after performing measurements.Method Descriptions
1.
__init__(self, data_path='data.json', train_path='train.json')
Description:
Initializes a new instance of the
Circuit
class.Parameters:
data_path
(str
, optional): Path to thedata.json
file. Default value is'data.json'
.train_path
(str
, optional): Path to thetrain.json
file. Default value is'train.json'
.Functionality:
None
or empty structures.Return:
2.
read_data(self)
Description:
Reads data from the
data.json
andtrain.json
files. Extracts basic circuit parameters and activation/training matrices.Parameters:
Functionality:
data.json
:data.json
.qubits
(number of qubits) anddepth
(circuit depth).2060
.1060
.train.json
:train.json
.activation_matrices
andtraining_matrices
.2061
.1061
).Return:
Error Handling:
1060
.3.
convert_input_data(self, option='empty', matrix_name=None)
Description:
Converts raw input data from JSON files into a structured form for circuit creation. There are two options:
'empty'
: Creates an empty activation matrix with zeros.'named'
: Processes a named activation matrix based onmatrix_name
.Parameters:
option
(str
, optional): Indicates which option to use.'empty'
for an empty matrix or'named'
for a named matrix. Default value is'empty'
.matrix_name
(str
, optional): Name of the activation matrix, required ifoption='named'
.Functionality:
'empty'
:[qubits][depth * 3]
._is_empty_matrix
).1064
is returned, and the method ends.'named'
:matrix_name
is provided. If not, error code1066
is returned.activation_matrices
based onmatrix_name
._is_matrix_consistent
).1063
is returned, and the method ends.qubit
) and each position (depth
), extracts the respective segments of activation and training values.input_data
with the following keys:Row
: Specifies the row where the 3-part pair is located.Position
: Specifies which 3-part pair it is in the row.ActivationTriple
: 3-part pair from the activation matrix.TrainingTriple
: 3-part pair from the training matrix.input_data
withcheck_input_data
.1061
is returned, andinput_data
is cleared.Return:
input_data
.Error Handling:
1064
: Inconsistent empty activation matrix.1063
: Inconsistent named activation matrix.1061
: Inconsistent formatted input data.4.
_is_matrix_consistent(self, matrix)
Description:
Verifies the consistency of a given activation matrix in terms of the number of rows, columns, and data types.
Parameters:
matrix
(list
): The matrix to be checked.Functionality:
depth * 3
).int
orfloat
).Return:
True
if the matrix is consistent, otherwiseFalse
.5.
_is_empty_matrix(self, matrix)
Description:
Checks if the given matrix consists solely of zeros.
Parameters:
matrix
(list
): The matrix to be checked.Functionality:
False
if any element is non-zero.True
if all elements are zero.Return:
True
if all elements are zero, otherwiseFalse
.6.
check_input_data(self)
Description:
Verifies the structure and consistency of
input_data
.Parameters:
Functionality:
input_data
is a list.qubits * depth
.Row
,Position
,ActivationTriple
,TrainingTriple
).int
forRow
andPosition
,list
forActivationTriple
andTrainingTriple
).ActivationTriple
andTrainingTriple
have exactly three elements.depth
.True
if all checks pass, otherwiseFalse
.Return:
True
ifinput_data
is consistent, otherwiseFalse
.7.
create_L_gate(self, entry)
Description:
This method creates the parameters for a single L-gate within the quantum circuit based on an entry from
input_data
. It processes the activation and training data, creates a circuit blueprint for the L-gate, and manages the incrementation of the current position and row. Throughout the process, success codes and error codes are returned to monitor execution status.Parameters:
entry
(dict
): A dictionary frominput_data
with the keys:Row
: Specifies the row where the 3-part pair is located.Position
: Specifies which 3-part pair it is in the row.ActivationTriple
: 3-part pair from the activation matrix.TrainingTriple
: 3-part pair from the training matrix.Functionality:
Entry Initialization and Validation:
a. Entry Validation:
entry
is a valid dictionary containing all required keys (Row
,Position
,ActivationTriple
,TrainingTriple
).2060
– Entry successfully validated.1060
– Invalid entry or missing keys.b. List Length Validation:
ActivationTriple
andTrainingTriple
are lists with exactly three elements.2061
– List lengths are correct.1061
– Invalid list lengths inActivationTriple
orTrainingTriple
.L-Gate Circuit Creation:
a. Data Extraction:
input_data
based on the currentRow
andPosition
.ActivationTriple
) and the training matrix (TrainingTriple
).2062
– Data successfully extracted.1062
– Error in data extraction.b. Data Merging:
P(ActivationValue 1)
P(TrainingValue 1)
H
P(ActivationValue 2)
P(TrainingValue 2)
H
P(ActivationValue 3)
P(TrainingValue 3)
2063
– Circuit blueprint successfully created.1063
– Error in merging data or creating the blueprint.Position and Row Incrementation:
a. Position Incrementation:
2064
– Position successfully incremented.1064
– Position exceeds the number of available 3-part pairs in the current row.b. Row Incrementation:
2065
– Row successfully incremented and switched to the next row.1065
– No more rows available ininput_data
(end of data structure).Circuit Blueprint and Status Code Return:
a. Successful Creation:
2060
– General success; specific codes like2061-2065
for intermediate factors.b. Error Case:
1060-1065
– Depending on the encountered error.Return:
Successful:
2060
to2065
) indicating the successful completion of individual steps.Example:
1
– The L-gate is applied to the qubit in the second row (index 1).true
– This indicates that this is the last operation in the current sequence of the L-gate.P(value)
,H
).Failed:
1060
to1065
) and a corresponding error message describing the reason for the failure.Example:
1061
– Invalid list lengths inActivationTriple
orTrainingTriple
.Intermediate Factors and Codes:
ActivationTriple
orTrainingTriple
.input_data
(end of data structure).Additional Notes:
Data structure of
input_data
:Row
: Specifies the row where the 3-part pair is located.Position
: Specifies which 3-part pair it is in the row.ActivationTriple
: A 3-part pair from the activation matrix.TrainingTriple
: A 3-part pair from the training matrix.Error Handling:
The method should ensure that when an error occurs, the corresponding error code is returned along with a clear error message to quickly identify and fix the issue.
Status Monitoring:
The use of success codes and error codes allows for precise monitoring and logging of the method’s state during execution, which is especially helpful for error diagnosis and debugging.
Further Processing:
After successful creation and incrementation, the generated circuit blueprint can be integrated into the main circuit (
self.circuit
) to form the complete quantum circuit.Sample Execution:
Successful L-Gate Creation
Input Data (
entry
):