Giter Club home page Giter Club logo

Comments (7)

refraction-ray avatar refraction-ray commented on May 29, 2024

Good question, since the topology of the circuit is a static information (shape information of the whole tensor network for the circuit), it is unable to jit in general even if you can use jax integer as the index number. However, in many interesting cases, depends on your specific use cases, you can find some way to smartly encode the problem and enable jit. So if you like, you can share your exact use case so that I can figure out whether the problem under simulation is jittable or not.

from tensorcircuit.

haimengzhao avatar haimengzhao commented on May 29, 2024

Hi @refraction-ray , thanks for the timely reply! I see why we must treat the topology as static. I'm okay with that. But since I'm inputing the topology as a list of indices, and list is non-hashable and therefore cannot be used as a static argument in jax.jit. Do you have any suggestions on how to work around on that? For example, my code is like (arch for architecture, a list of (i, j) specifying the positions to apply two qubit gates):

def apply_circuit(c, arch, unitary):
    assert len(arch) == len(unitary)
    for i in range(len(arch)):
        c.any(int(arch[i][0]), int(arch[i][1]), unitary=unitary[i])
    return c

Many thanks in advance!

from tensorcircuit.

refraction-ray avatar refraction-ray commented on May 29, 2024
# Apply X on given indices while jittable

n = 6
K = tc.set_backend("jax")

cand = K.stack([K.eye(2), tc.gates.pauli_gates[1].tensor])

def apply_gates(c, inds):
    inds = K.cast(inds, "int32")
    mpos = []
    for i in range(n):
        mpos.append(tc.gates.Gate(cand[inds[i]]))
    gate_applied = tc.quantum.QuOperator([m[0] for m in mpos], [m[1] for m in mpos])
    c.mpo(*range(n), mpo=gate_applied)
    return c

@K.jit
def cal_loss(inds):
    c = tc.Circuit(n)
    c = apply_gates(c, inds)
    for i in range(n-1):
        c.cx(i, i+1)
    return K.real(c.expectation_ps(z=[1]))

cal_loss(K.convert_to_tensor([0, 1, 0, 0, 1, 0]))

A smart way to around I just think of that might help, feel free to ask more if this paradigm not fits

from tensorcircuit.

haimengzhao avatar haimengzhao commented on May 29, 2024

Thanks @refraction-ray , this works well! I'm wondering how to generalize this to 2-qubit gates. I think we can use [0, 0, 0, 1, 2] to specify indices. But it's not clear how to consturct the mpos list while kepping cand an array. I tried the following

def apply_circuit(c, arch, unitary):
    assert len(arch) == len(unitary)
    iden = jnp.eye(2)
    n = c._nqubits
    for i in range(len(arch)):
        u = tc.gates.Gate(unitary[i].reshape(2, 2, 2, 2))
        cand = [tc.gates.Gate(iden), [u[0], u[2]], [u[1], u[3]]]
        mpos = [cand[arch[i, j]] for j in range(n)]
        mpo = tc.quantum.QuOperator([m[0] for m in mpos], [m[1] for m in mpos])
        c.mpo(*range(n), mpo=mpo)
    return c

but it's still non jittable, because cand is not an array. Any thoughts on that? Thanks!

from tensorcircuit.

refraction-ray avatar refraction-ray commented on May 29, 2024

two-qubit gate is more subtle, if the layout is more specified, say brickwall or ladder, i.e. the two qubit gates are always applied in adjacent indices, it is okay to jit using similar approaches. However, if all-to-all couplings are possible to apply two-qubit gates, for now, it is hard to efficiently jit (maybe there is also a clever way, just I fail to recongnize )

from tensorcircuit.

haimengzhao avatar haimengzhao commented on May 29, 2024

I see. Thanks for your suggestions!

from tensorcircuit.

refraction-ray avatar refraction-ray commented on May 29, 2024

btw, i will be at tsinghua U in 10-15 Aug, if you are there by then, we can meet offline. You can drop me an email for wechat id to connect if you are available.

from tensorcircuit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.