jcmgray / quimb

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

How to fix certain gates parameters and optimize the others #94

Open dh-dz opened 2 years ago

dh-dz commented 2 years ago

I am using FSIMG gates to build the quantum circuit, but want to only optimize theta parameter while fixing the other four as constants when running the optimizer. Is there a way to do that? Thank you!

jcmgray commented 2 years ago

Hmm, there's currently not a super simple way to do this but one possibility should be something like the following:

a) modify the tensors once the TN is constructed


from quimb.tensor.circuit import fsimg_param_gen

def fsimg_fixed(params, fixed_angles):
    theta = params[0]
    return fsimg_param_gen([theta, *fixed_angles])

# assuming tn == circ.uni etc.

for t in tn['FSIMG']:
    # directly update the `PTensor` with the partial fsimg generator
    t.params = t.params[:1]
    t.fn = functools.partial(fsimg_fixed, fixed_angles=t.params[1:])

b) construct the circuit manually with raw PArray gates

or in fact you can probably also something do:

from quimb.tensor.array_ops import PArray

for where in gates:

    G = PArray(
        fn=functools.partial(fsimg_fixed, fixed_angles=qu.randn(4)), 
        params=np.array([theta0])
    )
    circ.apply_gate_raw(G, where, tags='FSIMG_THETA')

I haven't tried either so they may need some tweaking, but let me know how you get on!