jcmgray / quimb

A python library for quantum information and many-body calculations including tensor networks.
http://quimb.readthedocs.io
Other
455 stars 107 forks source link

Applying 3 and 4-qubit gates to `CircuitMPS` #230

Closed PietropaoloFrisoni closed 2 months ago

PietropaoloFrisoni commented 2 months ago

What is your issue?

Good morning, I hope you are doing well.

I have a simple question regarding applying raw gates to CircuitMPS. In a nutshell, it seems to me that it does not work for 3 or 4 qubit gates.

The code below is a minimal example that shows the issue. I am importing the quantum gates from pennylane, but all that quimb receives is the corresponding matrix and the qubits it acts on.

Am I missing something? Thank you so much in advance for your time, and congratulations on developing quimb!

import pennylane as qml
import quimb.tensor as qtn

# case 1: one-qubit gate (works fine)
op1 = qml.PauliX(wires=[0])
qtn.CircuitMPS(N=1).apply_gate(op1.matrix(), *op1.wires, parametrize=None)

# case 2: two-qubit gate (works fine)
op2 = qml.CNOT(wires=[0, 1])
qtn.CircuitMPS(N=2).apply_gate(op2.matrix(), *op2.wires, parametrize=None)

# case 3: three-qubit gate (fails)
op3 = qml.Toffoli(wires=[0, 1, 2])
qtn.CircuitMPS(N=3).apply_gate(op3.matrix(), *op3.wires, parametrize=None)

# case 4: four-qubit gate (fails)
op4 = qml.DoubleExcitation(0.1, wires=[0, 1, 2, 3])
qtn.CircuitMPS(N=4).apply_gate(op4.matrix(), *op4.wires, parametrize=None)

Edit: I forgot to specify, but the error comes from line 1863 of tensor_1d.py: ValueError: too many values to unpack (expected 2). I am using quimb 1.7.3.

jcmgray commented 2 months ago

Hi @PietropaoloFrisoni, yes general 3+ qubit gates are not supported for MPS currently, though there are not fundamental barriers to implementing. See also #185.

I'm not sure I have time to implement this imminently, but the general flow would be:

  1. decompose the gate into an MPO (just via SVD) covering the minimal set of qubits
  2. gauge around this set of qubits
  3. contract and compress the set of qubits, possibly using the new tensor_network_1d_compress functionality (https://quimb.readthedocs.io/en/latest/autoapi/quimb/tensor/tensor_1d_compress/index.html)
PietropaoloFrisoni commented 2 months ago

Thanks again!

jcmgray commented 2 months ago

Multi-qubit gates are now handled by CircuitMPS via an MPO representation. It currently works via the dense representation so things like n-controlled gates which have a direct low-rank MPO representation are not implemented yet.

PietropaoloFrisoni commented 2 months ago

Awesome! Thanks a lot!

PietropaoloFrisoni commented 2 months ago

@jcmgray By the way, I notice that the error ValueError: too many values to unpack (expected 2) is still raised if we use the option: contract='swap+split'. Is it intentional? Namely, it seems to me that 3+ qubit gates expect the contract='nonlocal' option. If that's the case, then according to the documentation the difference between contract='auto-mps' and contract='swap+split' is only for 2-qubit gates? Thanks again for the quick implementation :+1:

jcmgray commented 2 months ago

Yes the swap+split method does not itself support 3+ qubit gates yet - though I'm sure there's a natural way to do it. Currently there is no difference between 'auto-mps' and 'swap+split', other than the first supports 3+ qubit gates! But it the future it might be more intelligent, for example, the nonlocal method is more expensive for a given max_bond / cutoff but achieves a better fidelity.

PietropaoloFrisoni commented 2 months ago

@jcmgray One last question: have you decided when quimb 1.8.1 will be officially released?

jcmgray commented 2 months ago

I will try and release a new version in the coming days.