项目作者: ZhengKeli

项目描述 :
BraAndKet is a library for numeral calculations of discrete quantum systems.(bra, ket, operator, trace ...)
高级语言: Python
项目地址: git://github.com/ZhengKeli/BraAndKet.git
创建时间: 2020-03-26T15:46:10Z
项目社区:https://github.com/ZhengKeli/BraAndKet

开源协议:MIT License

下载


BraAndKet

License

BraAndKet is a library for numeral calculations of discrete quantum systems.

Quickstart

Before Using

Please notice that this library is still actively developing. The stability and compatibility of APIs are NOT
guaranteed. Breaking changes are happening every day! Using this library right now, you may take your own risk.

Installation

You can install the latest release from PyPI.

  1. pip install braandket

Then you can import this library with name bnk

  1. import braandket as bnk

KetSpace

Any quantum states can exist in some space called Hilbert space. You can use bnk.KetSpace(n) to define such a space,
where n is its dimension. For example, to create a Hilbert space of a q-bit:

  1. qbit = bnk.KetSpace(2)
  2. print(qbit) # output: KetSpace(2)

You can define a name for a space using named parameter. The name is to describe this space when debugging. The name can
be a str, or any object to be printed out. When printed, the name of space will be shown, which is very helpful when
debugging.

  1. qbit_a = bnk.KetSpace(2, name="a")
  2. print(qbit_a) # output: KetSpace(2, name=a)
  3. qbit_b = bnk.KetSpace(2, name="b")
  4. print(qbit_b) # output: KetSpace(2, name=b)

You can call these 4 methods on a KetSpace instance to create ket vectors and operators:

  • method .eigenstate(k) - to get a ket vector, representing the k-th
    eigenstate
  • method .identity() - to get an identity operator in this Hilbert space
  • method .operator(k,b) - to get an operator
  • method .projector(k) - to get a projector
  1. ket_space = bnk.KetSpace(2)
  2. ket_vec = ket_space.eigenstate(0)
  3. identity_op = ket_space.identity()
  4. increase_op = ket_space.operator(1, 0)
  5. zero_proj = ket_space.projector(0)

A KetSpace is accompanied by a BraSpace. You can conveniently get it with .ct property. To avoid confusion, is not
allowed to create any vectors or operations with a BraSpace. Please do so with its corresponding KetSpace.
Calling .ct property, you can get back its KetSpace.

  1. ket_space = bnk.KetSpace(2)
  2. print(ket_space) # output: KetSpace(2)
  3. bra_space = ket_space.ct
  4. print(bra_space) # output: BraSpace(2)
  5. print(bra_space.ct is ket_space) # output: True

QTensors

QTensor is the basic type of computing elements in this library. A QTensor instance holds an np.ndarray as its
values and a tuple of Space instances. Each Space corresponds to an axis of the np.ndarray.

Any vectors, operators and tensors in quantum world are represented by QTensor. All vectors and operators mentioned
above are all QTensor instances.

  1. ket_space = bnk.KetSpace(2)
  2. ket_vec = ket_space.eigenstate(0)
  3. print(ket_vec)
  4. # output: QTensor(spaces=(KetSpace(2),), values=[1. 0.])
  5. identity_op = ket_space.identity()
  6. print(identity_op)
  7. # output: QTensor(spaces=(KetSpace(2), BraSpace(2)), values=[[1. 0.] [0. 1.]])
  8. increase_op = ket_space.operator(1, 0)
  9. print(increase_op)
  10. # output: QTensor(spaces=(KetSpace(2), BraSpace(2)), values=[[0. 0.] [1. 0.]])
  11. zero_proj = ket_space.projector(0)
  12. print(zero_proj)
  13. # output: QTensor(spaces=(KetSpace(2), BraSpace(2)), values=[[1. 0.] [0. 0.]])

You can easily get a conjugate transposed QTensor calling .ct property. It should be noted that sometimes, such
operation does not affect the values, but spaces.

  1. ket_space = bnk.KetSpace(2)
  2. ket_vec = ket_space.eigenstate(0)
  3. bra_vec = ket_vec.ct
  4. print(bra_vec)
  5. # output: QTensor(spaces=(BraSpace(2),), values=[1. 0.])
  6. increase_op = ket_space.operator(1, 0)
  7. decrease_op = increase_op.ct
  8. print(decrease_op)
  9. # output: QTensor(spaces=(BraSpace(2), KetSpace(2)), values=[[0. 0.] [1. 0.]])

QTensor instances can take tensor product using @ operator. They can automatically inspect which spaces to be
performed the “product-sum” (when the bra on the left meets the matching ket on the right), which to be remained.

Example1:

  1. qbit = bnk.KetSpace(2)
  2. amp = qbit.eigenstate(0).ct @ qbit.eigenstate(1)
  3. print(amp)
  4. # output: QTensor(spaces=(), values=0.0)

Example2:

  1. qbit_a = bnk.KetSpace(2, name="a")
  2. qbit_b = bnk.KetSpace(2, name="b")
  3. ket_vec_ab = qbit_a.eigenstate(0) @ qbit_b.eigenstate(1)
  4. print(ket_vec_ab)
  5. # output: QTensor(spaces=(KetSpace(2, name=a), KetSpace(2, name=b)), values=[[0. 1.] [0. 0.]])

Example3:

  1. qbit_a = bnk.KetSpace(2, name="a")
  2. qbit_b = bnk.KetSpace(2, name="b")
  3. tensor_ab = qbit_a.eigenstate(0).ct @ qbit_b.eigenstate(1)
  4. print(tensor_ab)
  5. # output: QTensor(spaces=(BraSpace(2, name=a), KetSpace(2, name=b)), values=[[0. 1.] [0. 0.]])

Example4:

  1. qbit = bnk.KetSpace(2)
  2. ket_vec_0 = qbit.eigenstate(0)
  3. ket_vec_1 = qbit.eigenstate(1)
  4. increase_op = qbit.operator(1, 0)
  5. result = increase_op @ ket_vec_0
  6. print(result)
  7. # output: QTensor(spaces=(KetSpace(2),), values=[0. 1.])
  8. print(result == ket_vec_1)
  9. # output: True

Contribution

This library is completely open source. Any contributions are welcomed. You can fork this repository, make some useful
changes and then send a pull request to me on GitHub.