Comments (3)
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.
Added a feature branch feature/convert
(2e36b44)
from qnet.
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)
- Applying a LocalProjector to a BasisKet does not return a simplified result HOT 1
- Need Pattern.finditer
- Allow CircuitSymbols labels to contain superscripts
- CircuitSymbols labels should always be rendered as text in Latex
- Intelligent feedback
- _flat_api_tools introduces too much abstraction for linters HOT 3
- Prevent instantiation of abstract base classes HOT 1
- LocalSpace.create crashes when using local_identifiers HOT 2
- .show() method fails HOT 10
- Use robust, cross-platform compatible file paths
- Officially support Windows HOT 2
- Adapt to change of free_symbols in sympy 1.2
- Add open_file utility function
- Hilbert space supermode factors and canonical transformations HOT 10
- Commutation relations are broken: distinguish finite and truncated basis dimension HOT 3
- Incorrect result for product of sums
- Indexed LocalSpaces must not be considered disjoint
- Write tests for symbolic_*
- How to use Pauli matrices in QNET?
- Error importing QNET package
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qnet.