Git Product home page Git Product logo

mpsim's Introduction

MPSim

Package for using Matrix Product States (MPS) to simulate quantum circuits.

Installation

After cloning the repository, run

pip install -e .

in the directory with setup.py.

Getting started

The main object is mpsim.MPS which defines a matrix product state of qudits initialized to the all zero state.

An MPS can be acted on by aribtrary one-qudit and two-qudit operations. Operations on >=3 qubits are not supported and must be compiled into a sequence of one- and two-qudit operations.

An MPSOperation consists of a gate and tuple of indices specifying which tensor(s) the gate acts on in the MPS. Gates must be expressed as TensorNetwork.Node objects with the appropriate number of edges. Some common gates are defined in mpsim.gates.

The following program initializes an MPS in the |00> state and prepares a Bell state.

import mpsim

mps = mpsim.MPS(nqudits=2)
mps.h(0)
mps.cnot(0, 1)

print(mps.wavefunction)
# Displays [0.70710677+0.j 0.        +0.j 0.        +0.j 0.70710677+0.j]

Two-qubit gate options

The number of singular values kept for a two-qubit gate can be set by the keyword argument maxsvals.

The following program prepares the same Bell state but only keeps one singular value after the CNOT.

import mpsim

mps = mpsim.MPS(nqudits=2)
mps.h(0)
mps.cnot(0, 1, maxsvals=1)

print(mps.wavefunction)
# Displays [0.70710674+0.j 0.        +0.j 0.        +0.j 0.        +0.j]

Note that the wavefunction after truncation is not normalized. An MPS can be renormalized at any time by calling the MPS.renormalize() method.

Cirq integration

Circuits defined in Cirq can be simulated with MPS as follows.

import cirq
from mpsim.mpsim_cirq.simulator import MPSimulator

# Define the circuit
qreg = cirq.LineQubit.range(2)
circ = cirq.Circuit(
    cirq.ops.H.on(qreg[0]),
    cirq.ops.CNOT(*qreg)
)

# Do the simulation using the MPS Simulator
sim = MPSimulator()
mps = sim.simulate(circ)
print(mps.wavefunction)
# Displays [0.70710677+0.j 0.        +0.j 0.        +0.j 0.70710677+0.j]

One can truncate singular values for two-qubit operations by passing in options to the MPSimulator.

sim = MPSimulator(options={"maxsvals": 1})
mps = sim.simulate(circ)
print(mps.wavefunction)
# Displays [0.70710674+0.j 0.        +0.j 0.        +0.j 0.        +0.j]

See help(MPSimulator) for a full list of options.

mpsim's People

Contributors

grmlarose avatar

Stargazers

Kazuki Tsuoka avatar Ryan LaRose avatar  avatar Zihe Wang avatar Daniel Strano avatar Tokarev Igor avatar uncertainsystems avatar  avatar Will Zeng avatar Zongkai avatar GuanYifei avatar Lei Wang avatar Victory Omole avatar Shixin Zhang avatar

Watchers

uncertainsystems avatar  avatar

mpsim's Issues

Add Matrix Product Operators for noisy simulation

An MPO on n qudits is just an MPS on 2n qudits. While this is the most straightforward generalization, it may not be the easiest in the long term. Considerations to evaluate this include

  • How to implement unitary channels (gates).
  • How to implement noise channels.

Update apply_one_qudit_gate for unitary/non-unitary operations

For non-unitary operations, SVD is required after the gate to orthonormalize the index.

Probably the best option is to add a method which orthonormalizes the index, then call this after the single-qudit gate contraction of the gate is non-unitary.

Add a software license

Having a license would help make the software more broadly useful and clarify when people can contribute / use this code.

Support MPS with qudits

Current support only includes qubits. Generalization to d-level systems will involve, minimally:

  • Specification of the dimension in MPS.__init__.
  • Gates for qudits.

Tensor contraction + SVD should be largely the same, if not identical.

Add option to create an MPS with an initial wavefunction

Could add as an argument to MPS.__init__ or as a new method, e.g.,

class MPS:
    ...
    @staticmethod
    def from_wavefunction(wavefunction: np.ndarray, qudit_dimension: int = 2) -> `MPS`:
        """Returns an MPS representation of the input wavefunction.

        Args:
            wavefunction: Initial wavefunction to write in MPS form.
            qudit_dimension: Dimension of qudits. (Default value is 2 for qubits.)
        """
        # Reshape the wavefunction to be a tensor with n = log_(qudit_dimension) (len(wavefunction))

        # Apply SVD to split all edges

        # Put into a list of nodes and return the MPS

Add support for different TensorNetwork backends

For example, tensorflow is not supported because, e.g., mpsim.gates.is_unitary assumes the node object has a .conj() method in the final line

return np.allclose(
    gate.conj().T @ gate, np.identity(gate.shape[0]), atol=1e-5
)

which is unique to the numpy backend. (This also explicitly uses numpy in other locations.)

To add support for different backends:

  1. Understand the best way to do this with tensornetwork.
  2. Add several tests for different backends.

Add MPS.expectation_of for expectation values

Potential signature.

class MPS:
  ...
  def expectation_of(operator: tn.Node, tensor_indices: Sequence[int]) -> np.real:
    """Returns the expectation value of the operator acting on the tensor indices."""

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.