ML (Matrix)
matrix_transpose()¶
Transpose an encrypted 2D matrix (swap rows and columns).
This cell verifies the matrix_transpose function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import matrix_transpose
def test_matrix_transpose(matrix):
return fhe.array(matrix_transpose(matrix))
compiler = fhe.Compiler(test_matrix_transpose, {'matrix': 'encrypted'})
inputset = [([[1, 2], [3, 4]],)]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("matrix_transpose tests passed! (1/1)")
dot_product()¶
Calculate the dot product of two encrypted arrays (vectors).
This cell verifies the dot_product function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import dot_product
import numpy as np
def test_dot_product(array1, array2):
res = dot_product(array1, array2)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_dot_product, {'array1': 'encrypted', 'array2': 'encrypted'})
inputset = [([1, 2], [3, 4]), ([-1, 0], [1, 1])]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = dot_product(inp[0], inp[1])
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected)
except Exception as e:
print(f"Skipping {inp}: {e}")
else:
_successes += 1
assert _successes > 0
print(f"dot_product tests passed! ({_successes}/{len(inputset)})")
matrix_add()¶
Perform element-wise addition of two encrypted matrices of the same dimensions.
This cell verifies the matrix_add function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import matrix_add
def test_matrix_add(matrix1, matrix2):
return fhe.array(matrix_add(matrix1, matrix2))
compiler = fhe.Compiler(test_matrix_add, {'matrix1': 'encrypted', 'matrix2': 'encrypted'})
inputset = [([[1, 2]], [[3, 4]])]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("matrix_add tests passed! (1/1)")
matrix_add tests passed! (1/1)
matrix_subtract()¶
Perform element-wise subtraction of two encrypted matrices of the same dimensions.
This cell verifies the matrix_subtract function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import matrix_subtract
def test_matrix_subtract(matrix1, matrix2):
return fhe.array(matrix_subtract(matrix1, matrix2))
compiler = fhe.Compiler(test_matrix_subtract, {'matrix1': 'encrypted', 'matrix2': 'encrypted'})
inputset = [([[1, 2]], [[3, 4]])]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("matrix_subtract tests passed! (1/1)")
matrix_subtract tests passed! (1/1)
matrix_multiply()¶
Perform matrix multiplication (dot product) of two encrypted matrices.
This cell verifies the matrix_multiply function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import matrix_multiply
def test_matrix_multiply(matrix1, matrix2):
return fhe.array(matrix_multiply(matrix1, matrix2))
compiler = fhe.Compiler(test_matrix_multiply, {'matrix1': 'encrypted', 'matrix2': 'encrypted'})
inputset = [([[1, 2]], [[3], [4]])]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("matrix_multiply tests passed! (1/1)")
matrix_elementwise_multiply()¶
Perform Hadamard (element-wise) multiplication of two encrypted matrices.
This cell verifies the matrix_elementwise_multiply function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import matrix_elementwise_multiply
def test_matrix_elementwise_multiply(matrix1, matrix2):
return fhe.array(matrix_elementwise_multiply(matrix1, matrix2))
compiler = fhe.Compiler(test_matrix_elementwise_multiply, {'matrix1': 'encrypted', 'matrix2': 'encrypted'})
inputset = [([[1, 2]], [[3, 4]])]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("matrix_elementwise_multiply tests passed! (1/1)")
matrix_vector_multiply()¶
Multiply an encrypted matrix by an encrypted vector.
This cell verifies the matrix_vector_multiply function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import matrix_vector_multiply
def test_matrix_vector_multiply(matrix, vector):
return fhe.array(matrix_vector_multiply(matrix, vector))
compiler = fhe.Compiler(test_matrix_vector_multiply, {'matrix': 'encrypted', 'vector': 'encrypted'})
inputset = [([[1, 2]], [3, 4])]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("matrix_vector_multiply tests passed! (1/1)")
matrix_exp()¶
Calculate the power of an encrypted square matrix.
This cell verifies the matrix_exp function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import matrix_exp
def test_matrix_exp(matrix):
return fhe.array(matrix_exp(matrix, 2))
compiler = fhe.Compiler(test_matrix_exp, {'matrix': 'encrypted', })
inputset = [([[1, 2], [3, 4]],)]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("matrix_exp tests passed! (1/1)")
covariance_matrix()¶
Calculate the covariance matrix of an encrypted 2D dataset.
This cell verifies the covariance_matrix function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import covariance_matrix
def test_covariance_matrix(matrix):
return fhe.array(covariance_matrix(matrix))
compiler = fhe.Compiler(test_covariance_matrix, {'matrix': 'encrypted'})
inputset = [([[1, 2], [3, 4], [5, 6]],)]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("covariance_matrix tests passed! (1/1)")
matrix_flatten()¶
Flatten a 2D encrypted matrix into a 1D encrypted array.
This cell verifies the matrix_flatten function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import matrix_flatten
def test_matrix_flatten(matrix):
return fhe.array(matrix_flatten(matrix))
compiler = fhe.Compiler(test_matrix_flatten, {'matrix': 'encrypted'})
inputset = [([[1, 2], [3, 4]],)]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("matrix_flatten tests passed! (1/1)")
tensor_flatten()¶
Flatten a 3D encrypted tensor into a 1D encrypted array.
This cell verifies the tensor_flatten function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.matrix import tensor_flatten
def test_tensor_flatten(tensor):
return fhe.array(tensor_flatten(tensor))
compiler = fhe.Compiler(test_tensor_flatten, {'tensor': 'encrypted'})
inputset = [([[[1, 2]]],)]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("tensor_flatten tests passed! (1/1)")