Quantum Principle Component Analysis

Possum_Hodgkin: <p>Hi there,</p>
<p>Just wondering if anyone in the pytorch community had come across this physics paper from 2020? They discovered a method of speeding up Principle Component Analysis  due to its equivalence to the Schrodinger equation.  Its only fully accurate up to second order, but higher order terms are generally small enough for it to matter less.  In many use cases a faster PCA is well worth a minor accuracy penalty.</p>
<aside class="onebox pdf" data-onebox-src="https://arxiv.org/pdf/2006.04379.pdf">
  <header class="source">

      <a href="https://arxiv.org/pdf/2006.04379.pdf" target="_blank" rel="noopener nofollow ugc">arxiv.org</a>
  </header>

  <article class="onebox-body">
    <a href="https://arxiv.org/pdf/2006.04379.pdf" target="_blank" rel="noopener nofollow ugc"><span class="pdf-onebox-logo"></span></a>

<h3><a href="https://arxiv.org/pdf/2006.04379.pdf" target="_blank" rel="noopener nofollow ugc">2006.04379.pdf</a></h3>

  <p class="filesize">3.54 MB</p>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>To try it out I went to Versatile Diffusion and substituted the elements of the torch.pca_lowrank() function for the schrodinger equation.</p>
<pre><code class="lang-auto">import torch

def solve_schrodinger_eq(u, s, v, potential_func, num_eigenstates):
    # Convert the variance to potential
    potential = potential_func(s)
    
    # Compute the inverse mass from the correlation kernel
    inverse_mass = torch.diag(s ** -1)
    
    # Compute the eigenvalues and eigenvectors of the Hamiltonian matrix
    hamiltonian = torch.mm(torch.mm(u, torch.diag(potential)), v.t()) + inverse_mass
    eigenvalues, eigenvectors = torch.symeig(hamiltonian, eigenvectors=True)
    
    # Sort the eigenvalues and eigenvectors in ascending order
    eigenvalues, indices = eigenvalues.sort()
    eigenvectors = eigenvectors[:, indices]
    
    # Select the num_eigenstates lowest-energy eigenvalues and eigenvectors
    eigenvalues = eigenvalues[:num_eigenstates]
    eigenvectors = eigenvectors[:, :num_eigenstates]
    
    # Normalize the eigenvectors
    norms = torch.sqrt(torch.sum(eigenvectors ** 2, dim=0))
    eigenvectors = eigenvectors / norms
    
    # Return the eigenvalues and eigenvectors (renamed as eigen-energies and eigenstates, respectively)
    return eigenvalues, eigenvectors
    
# Example usage
x_input = torch.rand(10, 10)
q = 0.5
niter = 10
num_eigenstates = 5

# Compute the PCA
u, s, v = torch.pca_lowrank(x_input, q=q, center=False, niter=niter)

# Define the potential function
potential_func = lambda x: -torch.log(x)

# Solve the Schrodinger equation
eigenvalues, eigenvectors = solve_schrodinger_eq(u, s, v, potential_func, num_eigenstates)

# Rename the eigenvectors as eigenstates
eigenstates = eigenvectors

# Print the results
print("Eigen-energies:", eigenvalues)
print("Eigenstates:", eigenstates)
</code></pre>
<p>The other beauty of this approach is it should work extremely quickly in qbits.</p>

Possum_Hodgkin: <p>The authors also did a neat little video where they explain what they found.</p>
<div class="youtube-onebox lazy-video-container" data-video-id="tPrxsgYU5sw" data-video-title="The Duality between Principal Component Analysis and Quantum Mechanics" data-provider-name="youtube">
  <a href="https://www.youtube.com/watch?v=tPrxsgYU5sw" target="_blank" rel="noopener nofollow ugc">
    <img class="youtube-thumbnail" src="https://img.youtube.com/vi/tPrxsgYU5sw/maxresdefault.jpg" title="The Duality between Principal Component Analysis and Quantum Mechanics" width="690" height="388">
  </a>
</div>


ptrblck: <p>Thanks for sharing this approach. I get a few errors trying to execute it:</p>
<pre><code class="lang-python">    R = torch.randn(n, q, dtype=dtype, device=A.device)

TypeError: randn(): argument 'size' must be tuple of ints, but found element of type float at pos 2
</code></pre>
<p>which points to the <code>q=0.5</code> argument. Also:</p>
<pre><code class="lang-python">RuntimeError: This function was deprecated since version 1.9 and is now removed. Please use the `torch.linalg.eigh` function instead.
</code></pre>
<p>is raised. Which PyTorch version did you use to execute the code?</p>

Possum_Hodgkin: <p>apologies. ptrblack.  Here is a direct comparison in 1.12.0</p>
<pre><code class="lang-python">import torch
import time

# Create some test data.
variances = torch.tensor([1.0, 2.0, 3.0])
correlation_kernel = torch.tensor([0.5, 0.3, 0.2])

def schrodinger_pca(variances, correlation_kernel):
  """
  Runs the Schrödinger equation for a quantum steady state problem.

  Args:
    variances: A tensor of variances.
    correlation_kernel: A tensor of correlation kernels.

  Returns:
    A tuple of eigen-energies and eigenstates.
  """

  # Convert the variances and correlation kernels to Schrödinger equation parameters.
  potential = torch.diag(variances)
  inverse_mass = torch.diag(1.0 / correlation_kernel)

  # Solve the Schrödinger equation.
  eigen_energies, eigenstates = torch.linalg.eigh(potential @ inverse_mass)

  return eigen_energies, eigenstates
  
 
  
  # Run the Schrödinger PCA function.
start = time.time()
eigen_energies_schrodinger, eigenstates_schrodinger = schrodinger_pca(variances, correlation_kernel)
end = time.time()
time_schrodinger = end - start

# Run the original PCA function.
start = time.time()
A = torch.diag(variances) @ torch.diag(1.0 / correlation_kernel)
pca = torch.pca_lowrank(A)
end = time.time()
time_pca = end - start

# Print the results.
print("Schrödinger Egeinenergies", eigen_energies_schrodinger)
print("Schrödinger Egein states", eigenstates_schrodinger)
# Print the eigenvalues.
print("PCA Eigenvalues:", pca[0])

# Print the eigenvectors.
print("PCA Eigenvectors:", pca[1])

# Print the results.
print("Schrödinger PCA time:", time_schrodinger)
print("Lowrank PCA time", time_pca)

# Compare the results.
print("Are the results the same?:", torch.allclose(eigen_energies_schrodinger,  pca[0]))
eigenstates_schrodinger ,
</code></pre>

Possum_Hodgkin: <p>The key thing I’m conveying here is how the same inputs can be used for both calculations. There is a bunch of existing literature on quantum PCA, but seeing this paper made me realise it was applicable to pytorch tensors using our existing code bases.</p>
<p>When you run this code you’ll see the results aren’t an exact match. The schrodinger version is giving back exact principal components not estimated weightings. My understanding is that the larger the sample data the closer the lowrank function’s estimate will get to the schrodinger answer.</p>
<p>In addition to exact answer, the key benefit of the Schrodinger version is that it is very amenable to calculation on Qbits, since it is literally a quantum waveform calculation. This is such an advantage that the speed advantages of lowrank are eclipsed and you’re left with a solution which is both more accurate and faster. This is because wave form collapse of a Qbit array doesn’t scale as slowly with input vectors, they are computed simultaneously, so very large input datasets can be processed near instantly.</p>
<p>In the case of a workload like AI training this may drastically reduce the training and processing times. Theoretically by orders of magnitude in some circumstances. There are similar applications for climate modelling, financial modelling or other high dimensionality problems.</p>
<p>For all these use cases it seems a fairly simply pytorch interface layer could shift a lot of the workload from classical to quantum computing using an existing codebase.</p>

Possum_Hodgkin: <p>This approach would apply directly to Microsoft’s recent advances on 1 Bit transformers, since schrodinger’s equation returns 1 bit ternary outputs</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://arxiv.org/abs/2310.11453">
  <header class="source">
      <img src="https://arxiv.org/static/browse/0.3.4/images/icons/favicon-32x32.png" class="site-icon" width="32" height="32">

      <a href="https://arxiv.org/abs/2310.11453" target="_blank" rel="noopener nofollow ugc">arXiv.org</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/402;"><img src="https://static.arxiv.org/icons/twitter/arxiv-logo-twitter-square.png" class="thumbnail" width="500" height="500"></div>

<h3><a href="https://arxiv.org/abs/2310.11453" target="_blank" rel="noopener nofollow ugc">BitNet: Scaling 1-bit Transformers for Large Language Models</a></h3>

  <p>The increasing size of large language models has posed challenges for deployment and raised concerns about environmental impact due to high energy consumption. In this work, we introduce BitNet, a scalable and stable 1-bit Transformer architecture...</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>