How to write a long equation with a long vector(s) shortly?
2 views (last 30 days)
Show older comments
Antti Sällinen
on 30 Jun 2020
Commented: Antti Sällinen
on 1 Jul 2020
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/324529/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/324532/image.png)
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.
1 Comment
dpb
on 30 Jun 2020
Attach a dataset with input/expected outputs...
I don't see why can't just be vectorized; maybe I'm missing something by not seeing the actual data structure.
Accepted Answer
Vashist Hegde
on 30 Jun 2020
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
3 Comments
More Answers (1)
dpb
on 30 Jun 2020
Edited: dpb
on 30 Jun 2020
Vashist has right idea -- wil have to have a real name for the function/function file as can't be a multi-line anonymous function (unfortunately, ML syntax doesn't support the idea).
Also, you can simplify slightly if you augment your x vector with the first unity coefficient--
function S=optimfunc(x,m)
% computes summation x*m for optimization objective function
x=[1 x]; % now has same length as the matrix array
S=0;
for i=1:numel(x)
S=S+x(i)*m{i};
end
S=trace(sqrtm(S/4));
end
This does the augmentation inside the function; might be more efficient to do outside just once.
Depending on the use, may have to wrap the above in an anonymous function that has the needed footprint for the optimization routine you're using -- there are examples of doing that in the documentation/examples.
See Also
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!