MATLAB: How to write a long equation with a long vector(s) shortly

equationMATLABOptimization Toolboxvectors

Hi!
I am currently doing a optimizations on randomly generated density matrices and those matrices can be pretty big, dimensions being where in my case, n stands for amount of qubits one wants to use. For example for three qubits the density matrix would be -matrix. My problem comes from the function I want to optimize.
One can write a density matrix as a sum of product of the expectation value of a matrix, and the related matrix, where these matrices are a matrices of the Pauli basis in this particular dimension. Lets say that the expectation values I want to optimize are in the vector x and the related matrices that are known are in a cell pauli_new{k}.
Pauli_new{} is created in another function, if thats related information.
Now for a two qubit system this density matrix could be written as
1/4 * (pauli_new{1} + x(1)*pauli_new{2} + x(2)*pauli_new{3} + x(3)*pauli_new{4} ...
+ x(4)*pauli_new{5} + x(5)*pauli_new{6} + x(6)*pauli_new{7} + ...
x(7)*pauli_new{8} + x(8)*pauli_new{9} + x(9)*pauli_new{10} + ...
x(10)*pauli_new{11} + x(11)*pauli_new{12} + x(12)*pauli_new{13} + ...
x(13)*pauli_new{14} + x(14)*pauli_new{15} + x(15)*pauli_new{16})
Here the vector x is shorter than the cell just because x(1) has always a value of 1. Also, the values of x(n) are scalars and pauli_new{n} are matrices, if it did not come clear.
My question now: Is there any possible way to write these kind of equations in a shorter way? My goal for optimizations is to optimize like a five qubit systems, which then contains 1024 elements, and then the vector x would have 1023 elements and pauli_new{} would have 1024 elements. I would not be stoked to write over thousand terms by my hands (would never do it).
And to add there little more difficulty, this equation should be formed as a function like I have done it in my two qubit optimization:
f = @(x) trace(sqrtm(1/4 * (pauli_new{1} + x(1)*pauli_new{2} + x(2)*pauli_new{3} + x(3)*pauli_new{4} ...
+ x(4)*pauli_new{5} + x(5)*pauli_new{6} + x(6)*pauli_new{7} + ...
x(7)*pauli_new{8} + x(8)*pauli_new{9} + x(9)*pauli_new{10} + ...
x(10)*pauli_new{11} + x(11)*pauli_new{12} + x(12)*pauli_new{13} + ...
x(13)*pauli_new{14} + x(14)*pauli_new{15} + x(15)*pauli_new{16})' ...
* (1/4 * (pauli_new{1} + x(1)*pauli_new{2} + x(2)*pauli_new{3} + x(3)*pauli_new{4} ...
+ x(4)*pauli_new{5} + x(5)*pauli_new{6} + x(6)*pauli_new{7} + ...
x(7)*pauli_new{8} + x(8)*pauli_new{9} + x(9)*pauli_new{10} + ...
x(10)*pauli_new{11} + x(11)*pauli_new{12} + x(12)*pauli_new{13} + ...
x(13)*pauli_new{14} + x(14)*pauli_new{15} + x(15)*pauli_new{16}))));
The function above works just as I want to and gives good outcomes. I also think that the cell array for pauli basis is not the best way to go, since for example the built-in function for summation does not do well with cell arrays, so I would be appreciated if you can suggest something else.
In conclusion: how to write an equation that contains a long vector(s) simply or shortly?
Thank you for your interest.
EDIT: Added information about the vector x and cell pauli_new.

Best Answer

I think this should work just fine considering pauli_new{k} are matrices
function f = @(x,pauli_new)
sum = pauli_new{1};
for i = 1:16
sum = sum +(x(i-1)*pauli_new{i});
end
f = trace(sqrtm((1/4)*sum));
end