Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Antti Sällinen
Antti Sällinen le 30 Juin 2020
Commenté : Antti Sällinen le 1 Juil 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.
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 commentaire
dpb
dpb le 30 Juin 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.

Connectez-vous pour commenter.

Réponse acceptée

Vashist Hegde
Vashist Hegde le 30 Juin 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 commentaires
dpb
dpb le 1 Juil 2020
Good catch, Walter...didn't notice.
Antti Sällinen
Antti Sällinen le 1 Juil 2020
Thank you very much! This was last problem I had to tackle and now my project works fine.
I had in fact tried using for-loop, but I didn't manage to get it working. I am not very good with variables in Matlab. Also because of this answer I learned about functions and their calling more.
Once more, thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 30 Juin 2020
Modifié(e) : dpb le 30 Juin 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.
  1 commentaire
Antti Sällinen
Antti Sällinen le 1 Juil 2020
Gotta thank you too for the answer similar to one above!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by