# beittech/Quasar

Simplify writing quantum assembly code
Python Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information. CHANGELOG.md LICENSE.txt README.md qfourier.py qgrover.py quasar.py qutils.py

# Quasar 1.0.1

Quasar is a utility tool to simplify writing quantum assembly code. It helps translate high level `if` statements into a sequence of controlled quantum commands. It can be easily integrated with IBM Qiskit, Google Cirq or OpenQASM.

Express your thoughts in a high-level way, let Quasar take care about details.

# Features

At the beginning of each program the `Program` object has to be created

``````prgm = Program()
``````

Single qubit object can be created as follow

``````qbit_0 = prgm.Qubit()
``````

An initial value for a qubit can also be provided

``````qbit_1 = prgm.Qubit( 1 )
``````

Many qubits can be created in one statement

``````qubits = prgm.Qubits( [ 0, 1, 0, 1, 0, 1, 0, 1 ] )
``````

Instructions are composed either with `+=` and `+` operators

``````prgm += X( qbit_0 ) + Y( qbit_1 ) + Z( qubits )
``````

Or with a list syntax

``````prgm += [ X( qbit_0 ) , H( qbit_1 ) , Z( qubits ) ]
``````

Quasar simplifies writing `if` statements. It looks almost like in any high-level programming language

``````If( ... ).Then( ... )
If( ... ).Then( ... ).Else( ... )
``````

For example

``````If( All( qbit_1 ) ).Then(
X( qubit_0 )
)
``````

Inside an `if` statements some functions can be used

`Any( qubits: List[ Qubit ] ) -> Bool`, at least one qubit has to be set to 1

``````If( Any( qubits, qubits, qubits ) )
``````

`Zero( qubits: List[ Qubit ] ) -> Bool`, all qubits have to be set to 0

``````If( Zero( qubits, qubits, qubits ) )
``````

`All( qubits: List[ Qubit ] ) -> Bool`, all qubits has to be set to 1

``````If( All( qubits, qubits, qubits ) )
``````

`Match( qubits : List[ Qubit ], mask: List[int] ) -> Bool`, all qubits has to be set as in a mask

``````If( Match( [qubits, qubits, qubits, qubits], mask=[1, 0, 1, 0] ) )
``````

To simplify writing Grover algorithm `If(...).Flip()` statement is provided. `Flip` make phase inversion for all states which satisfy the condition.

``````If( All( qubits, qubits, qubits ).Flip()
``````

# Installation and Requirements

Quasar is a single python file script. It does not have any non-standard dependencies. It is required to have python >= 3.7

# Extension

To generate an assembly code in your own format just define an implementation of the `IQAsmFormatter` class

# Quasar to IBM Qiskit intagration

```# Quasar

from quasar import All, H, If, Program, Quasar, X

prgm = Program()
q_bits = prgm.Qubits([0, 0])
c_bits = prgm.CBits(2)

prgm += X(q_bits)
prgm += If(All(q_bits)).Then(
H(q_bits)
)

# Quasar -> OpenQASM

qasm_str = Quasar().to_qasm_str(prgm)

# OpenQASM -> Qiskit

from qiskit import execute, Aer, QuantumCircuit

circuit = QuantumCircuit.from_qasm_str(qasm_str)
result = execute(circuit, Aer.get_backend('statevector_simulator')).result()
print(result.get_statevector(circuit, decimals=3))```

# Syntax examples

```def example_nested_if() -> None:
prgm = Program()

q_vars = prgm.Qubits(32 * )

prgm += If(All([q_vars, q_vars, q_vars, q_vars])).Then(
X(q_vars) +
If(Not(All(q_vars))).Then(
X(q_vars) + X(q_vars)
).Else(
If(All(q_vars)).Then(
[X(q_vars), X(q_vars)]
).Else(
[X(q_vars), X(q_vars)]
)
)
).Else(
If(Not(All(q_vars))).Then(
X(q_vars) + X(q_vars)
).Else(
If(All(q_vars)).Then(
X(q_vars) +
X(q_vars) +
If(Not(All(q_vars))).Then(
X(q_vars) + X(q_vars)
).Else(
If(All(q_vars)).Then(
[X(q_vars), X(q_vars)]
).Else(
[X(q_vars), X(q_vars)]
)
)
).Else(
If(Not(All(q_vars))).Then(
X(q_vars) + X(q_vars)
).Else(
If(All(q_vars)).Then(
[X(q_vars), X(q_vars)]
).Else(
[X(q_vars)]
)
)
)
)
)

```def example_measurement() -> None:
prgm = Program()

q_vars = prgm.Qubits([1, 0, 1, 0, 0])
c_vars = prgm.CBits(5)

prgm += (
CX(q_vars, q_vars) +
If(All(q_vars)).Then(
If(All(q_vars)).Then(
CX(q_vars, q_vars)
)
) +
Measurement(q_vars, c_vars)
)

```def example_grover() -> None:
def Grover(
qubits: List[Qubit],
predicate: ASTNode
) -> Program:
prgm = Program()

for qbit in qubits:
prgm += H(qbit)

# IBM QisKit http://tiny.cc/wn7uaz
number_of_iters_1 : Callable[[int], int] = \
lambda size: int(sqrt(2 ** size))

# arxiv http://tiny.cc/mo7uaz
number_of_iters_2 : Callable[[int], int] = \
lambda size: int(pi / 4 / asin(sqrt(1 / (2 ** size))))

for _ in range(number_of_iters_1(len(qubits))):
prgm += If(predicate).Flip()

for qbit in qubits:
prgm += H(qbit)

prgm += If(Zero(qbits)).Flip()

for qbit in qbits:
prgm += H(qbit)

prgm += Flip(qbits)

return prgm

prgm = Program()
qubits = prgm.Qubits([0, 0, 0])
prgm += Grover(qubits, predicate=Match(qubits, mask=[0, 1, 0]))
```def example_fourier() -> None:
def Fourier(
qubits: List[Qubit]
) -> Program:
prgm = Program()

length = len(qubits)

for i in range(length):
prgm += H(qubits[i])

for j in range(2, length + 1 - i):
prgm += If(All(qubits[i + j - 1])).Then(
Phase(qubits[i], 2 * pi / (2 ** j))
)

for i in range(length // 2):
prgm += CNot(qubits[i], qubits[length - i - 1])
prgm += CNot(qubits[length - i - 1], qubits[i])
prgm += CNot(qubits[i], qubits[length - i - 1])

return prgm

prgm = Program()
qubits = prgm.Qubits([0, 1])
prgm += Fourier(qubits)