Giter VIP home page Giter VIP logo

Comments (3)

goerz avatar goerz commented on July 30, 2024

I would propose to generalize this to a sub-package convert that very generally collects modules for converting QNET expressions to alternative formats. This would include serialization formats (i.e., data structures that can directly be written to json or xml, providing a textual and parsable export of an expression), but also conversion to e.g. sympy and qutip, and possibly C++/Fortran, and Julia-related formats in the future.

As part of this, I would refactor the to_qutip methods of all the qnet objects. I'm not a huge fan of solving this through object orientation, as we are doing now. I find that it makes it a bit hard to get a clear picture of how conversion to qutip works, as the relevant code is scattered throughout all files. Also, there isn't anything inherently special about a conversion to qutip vs the other formats mentioned above (and I don't really see us adding similar methods to all the qnet objects for conversion to other formats). Instead of using methods, I would propose a single routine convert_to_qutip, which has all the conversion code in one place.

I've just written such a routine for a conversion to sympy matrices (qnet.misc.to_sympy_matrix.convert_to_sympy_matrix), which is basically a copy of the qutip conversion code, with small modification. I find this to be quite concise, and refactoring the existing qutip-conversion code shouldn't take more than an afternoon. It would however break backwards compatibility pretty drastically (unless maybe we keep the to_qutip methods as a wrapper, just for maintaining backwards compatibility. We could then drop them when we're ready to release a new major version at some point in the future)

Thus, for now, we would end up with two new modules:

qnet.convert.to_qutip
qnet.convert.to_sympy_matrix

The to_qutip module would also contain functions such as the recently added represent_symbols_as.

Something like qnet.convert.to_json would be next in line to address the original issue of a human-readable and easy to parse serialization format.

I would also consider moving parts (or even all of) of the qnet.misc.qsd_codegen module to qnet.convert.to_cpp respectively qnet.convert.to_qsd. I recently found out that sympy contains pretty sophisticated C and Fortran code generation routines, so it would seem reasonable to allow for conversion of arbitrary QNET expressions to C++/Fortran code.

from qnet.

goerz avatar goerz commented on July 30, 2024

Added a feature branch feature/convert (2e36b44)

from qnet.

goerz avatar goerz commented on July 30, 2024

The recently implemented new printing system (#30) allows to very generally generate textual representation of any expression. This includes tree-like structures that would be suitable to being written to a text file (and that can be re-instantiated by simple evaluation). E.g. from the test of the new qnet.printing.srepr function:

>>> hs1 = LocalSpace('q_1', basis=('g', 'e'))
>>> hs2 = LocalSpace('q_2', basis=('g', 'e'))
>>> ket_g1 = BasisKet('g', hs=hs1)
>>> ket_e1 = BasisKet('e', hs=hs1)
>>> ket_g2 = BasisKet('g', hs=hs2)
>>> ket_e2 = BasisKet('e', hs=hs2)
>>> bell1 = (ket_e1 * ket_g2 - I * ket_g1 * ket_e2) / sympy.sqrt(2)
>>> srepr(bell1, indented=True)
ScalarTimesKet(
    Mul(Rational(1, 2), Pow(Integer(2), Rational(1, 2))),
    KetPlus(
        TensorKet(
            BasisKet(
                'e',
                hs=LocalSpace(
                    'q_1',
                    basis=('g', 'e'),
                    dimension=2,
                ),
            ),
            BasisKet(
                'g',
                hs=LocalSpace(
                    'q_2',
                    basis=('g', 'e'),
                    dimension=2,
                ),
            ),
        ),
        ScalarTimesKet(
            Mul(Integer(-1), I),
            TensorKet(
                BasisKet(
                    'g',
                    hs=LocalSpace(
                        'q_1',
                        basis=('g', 'e'),
                        dimension=2,
                    ),
                ),
                BasisKet(
                    'e',
                    hs=LocalSpace(
                        'q_2',
                        basis=('g', 'e'),
                        dimension=2,
                    ),
                ),
            ),
        ),
    ),
)

In the future, something like a JSON or XML printer could easily be added as an appropriate sublass of qnet.printing.Printer, if there is demand for this.

from qnet.

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.