Code Monkey home page Code Monkey logo

qaqarot's Introduction

qaqarot

The blueqat project has been renamed the qaqarot project because of the branding strategy of blueqat inc.

Logo

blueqat

A Quantum Computing SDK

Version

Version

Tutorial

https://github.com/Blueqat/Blueqat-tutorials

Notice

The back end has been changed to tensor network. The previous backend environment can still be used with .run(backend="numpy").

Install

git clone https://github.com/Blueqat/Blueqat
cd Blueqat
pip3 install -e .

or

pip3 install blueqat

Circuit

from blueqat import Circuit
import math

#number of qubit is not specified
c = Circuit()

#if you want to specified the number of qubit
c = Circuit(50) #50qubits

Method Chain

# write as chain
Circuit().h[0].x[0].z[0]

# write in separately
c = Circuit().h[0]
c.x[0].z[0]

Slice

Circuit().z[1:3] # Zgate on 1,2
Circuit().x[:3] # Xgate on (0, 1, 2)
Circuit().h[:] # Hgate on all qubits
Circuit().x[1, 2] # 1qubit gate with comma

Rotation Gate

Circuit().rz(math.pi / 4)[0]

Run

from blueqat import Circuit
Circuit(50).h[:].run()

Run(shots=n)

Circuit(100).x[:].run(shots=100)
# => Counter({'1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111': 100})

Single Amplitude

Circuit(4).h[:].run(amplitude="0101")

Expectation value of hamiltonian

from blueqat.pauli import Z
hamiltonian = 1*Z[0]+1*Z[1]
Circuit(4).x[:].run(hamiltonian=hamiltonian)
# => -2.0

Blueqat to QASM

Circuit().h[0].to_qasm()
    
#OPENQASM 2.0;
#include "qelib1.inc";
#qreg q[1];
#creg c[1];
#h q[0];

Hamiltonian

from blueqat.pauli import *

hamiltonian1 = (1.23 * Z[0] + 4.56 * X[1] * Z[2]) ** 2
hamiltonian2 = (2.46 * Y[0] + 5.55 * Z[1] * X[2] * X[1]) ** 2
hamiltonian = hamiltonian1 + hamiltonian2
print(hamiltonian)
    
# => 7.5645*I + 5.6088*Z[0]*X[1]*Z[2] + 5.6088*X[1]*Z[2]*Z[0] + 20.793599999999998*X[1]*Z[2]*X[1]*Z[2] + 13.652999999999999*Y[0]*Z[1]*X[2]*X[1] + 13.652999999999999*Z[1]*X[2]*X[1]*Y[0] + 30.8025*Z[1]*X[2]*X[1]*Z[1]*X[2]*X[1]

Simplify the Hamiltonian

hamiltonian = hamiltonian.simplify()
print(hamiltonian)

#=>-2.4444000000000017*I + 27.305999999999997j*Y[0]*Y[1]*X[2] + 11.2176*Z[0]*X[1]*Z[2]

QUBO Hamiltonian

from blueqat.pauli import qubo_bit as q

hamiltonian = -3*q(0)-3*q(1)-3*q(2)-3*q(3)-3*q(4)+2*q(0)*q(1)+2*q(0)*q(2)+2*q(0)*q(3)+2*q(0)*q(4)
print(hamiltonian)
    
# => -5.5*I + 1.0*Z[1] + 1.0*Z[2] + 1.0*Z[3] + 1.0*Z[4] + 0.5*Z[0]*Z[1] + 0.5*Z[0]*Z[2] + 0.5*Z[0]*Z[3] - 0.5*Z[0] + 0.5*Z[0]*Z[4]

Time Evolution

hamiltonian = [1.0*Z(0), 1.0*X[0]]
a = [term.get_time_evolution() for term in hamiltonian]

time_evolution = Circuit().h[0]
for evo in a:
    evo(time_evolution, np.random.rand())
    
print(time_evolution)

# => Circuit(1).h[0].rz(-1.4543063361067243)[0].h[0].rz(-1.8400416676737137)[0].h[0]

QAOA

from blueqat import Circuit
from blueqat.utils import qaoa
from blueqat.pauli import qubo_bit as q
from blueqat.pauli import X,Y,Z,I

hamiltonian = q(0)-q(1)
step = 1

result = qaoa(hamiltonian, step)
result.circuit.run(shots=100)
    
# => Counter({'01': 99, '11': 1})

Circuit Drawing Backend

from blueqat import vqe
from blueqat.pauli import *
from blueqat.pauli import qubo_bit as q

#hamiltonian = q(0)-3*q(1)+2*q(0)*q(1)+3*q(2)*q(3)+q(4)*q(7)
hamiltonian = Z[0]-3*Z[1]+2*Z[0]*Z[1]+3*Z[2]*Z[3]+Z[4]
step = 8

result = vqe.Vqe(vqe.QaoaAnsatz(hamiltonian, step)).run()
result.circuit.run(backend='draw')

draw

Cloud System Connection (API Key is required)

from bqcloud import register_api
api = register_api("Your API Key")

from bqcloud import load_api
api = load_api()

from blueqat import Circuit
from bqcloud import Device

task = api.execute(Circuit().h[0].cx[0, 1], Device.IonQDevice, 10)
#task = api.execute(Circuit().h[0].cx[0, 1], Device.AspenM1, 10)

# Wait 10 sec. If complete, result is returned, otherwise, None is returned.
result = task.wait(timeout=10)

if result:
    print(result.shots())
else:
    print("timeout")

Document

https://blueqat.readthedocs.io/en/latest/

Contributors

Contributors

Disclaimer

Copyright 2022 The Blueqat Developers.

qaqarot's People

Contributors

derbuihan avatar frankfeenix avatar gm3d avatar gyu-don avatar koichi-tsujino avatar minatoyuichiro avatar ryunagai avatar shinmorino avatar ssmi1975 avatar whitesymmetry avatar ymurata avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

qaqarot's Issues

Connect with MDR Quantum Cloud

Add backend for MDR Quantum Cloud.
Token management, setup JSON, post JSON, receive JSON and parse as result features are required.

Sympy Unitary Matrix Backend

The backend which returns sympy's unitary matrix.

Circuit().h[0].run(backend="sympy_unitary")
# => Matrix([
# => [sqrt(2)/2,  sqrt(2)/2],
# => [sqrt(2)/2, -sqrt(2)/2]])

Requrements:

  • Use Sympy's Matrix.
  • When sympy is not installed, this backend is unavailable however Blueqat and other backends are available.
  • At least, following gates shall be implemented: X/Y/Z/U3/RX/RY/RZ/CX/CZ
  • Unit test

Optional Requirements:

  • Sampling (run(shots=...))
  • Pretty output for Jupyter Notebook

VQE: Count the number of calling sampler.

For What?

It is convenient to estimate running time on other backend and adjustment optimizer.

What are the concerns?

It may decrease speed.
Vqe.run() may called multiple times.

Well-typed running

Implement

  • Circuit.statevec() -> np.ndarray
  • Circuit.shots() -> Counter[str, int]
  • Circuit.oneshot() -> Tuple[np.ndarray, str]

expect: keys are reversed (vqe branch)

In [4]: vqe.non_sampling_sampler(Circuit().x[0].i[2], (0, 1))                                                                                                                                
Out[4]: {(0, 0): 0.0, (0, 1): 1.0, (1, 0): 0.0, (1, 1): 0.0}
  • bad
    • {(0, 0): 0.0, (0, 1): 1.0, (1, 0): 0.0, (1, 1): 0.0}
  • better
    • {(0, 0): 0.0, (1, 0): 1.0, (0, 1): 0.0, (1, 1): 0.0}
  • good
    • {(1, 0): 1.0}

Local install error

Local instllation isn't be successful. setup.py emerges some errors.

(base) C:\Users\s1430\Downloads\Blueqat-master_test\Blueqat-master>pip install -e .
Obtaining file:///C:/Users/s1430/Downloads/Blueqat-master_test/Blueqat-master
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\s1430\Downloads\Blueqat-master_test\Blueqat-master\setup.py", line 4, in
long_description = f.read()
UnicodeDecodeError: 'cp932' codec can't decode byte 0x84 in position 1477: illegal multibyte sequence

----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in C:\Users\s1430\Downloads\Blueqat-master_test\Blueqat-master\

I want to modify blueqat locally for emergent business. I wish this problem will be solved soon.

Refactoring for Circuit class

Now, Circuit class has 3 functionalities.

  • Interface to build and run the circuit
  • Own the backends and dispatch to them
  • Own the gates for the circuit

These features shall be separated.

IBMQ Backend: QasmSimulator will be deprecated

tests/test_ibmq_backend.py::test_ibmq_backend
  /..../lib/python3.9/site-packages/qiskit/providers/aer/backends/qasm_simulator.py:329: PendingDeprecationWarning: The `QasmSimulator` backend will be deprecated in the future. It has been superseded by the `AerSimulator` backend.

Numba backend returns wrong statevector

from blueqat import Circuit

c = Circuit()
c.h[0]
c.cx[0, 1];
c.z[2];

import numpy as np

v1 = c.run_with_numpy()
v2 = c.run_with_numba()
v3 = c.run_with_qgate()

print('numpy')
print(v1)
print('numba')
print(v2)
print('qgate')
print(v3)

Expected: All backends return same result
Actual:

numpy
[0.70710678+0.j 0.        +0.j 0.        +0.j 0.70710678+0.j
 0.        +0.j 0.        +0.j 0.        +0.j 0.        +0.j]
numba
[ 0.70710678+0.j  0.70710678+0.j  0.        +0.j  0.        +0.j
 -0.        +0.j -0.        +0.j -0.        +0.j -0.        +0.j]
qgate
[0.70710678+0.j 0.        +0.j 0.        +0.j 0.70710678+0.j
 0.        +0.j 0.        +0.j 0.        +0.j 0.        +0.j]

Docker version.

When will you rerease docker version? It will be helpful for some offices.

Ancilla macro

with c.ancilla() as a:
  c.cx[0, a[0]]
  c.cx[0, a[0]]

with c.ancilla(pos=4, stop=6, reset=True) as a:
  c.cx[3, a[0]]

Why cu3(0,0,a) doesn't match with cu1(a) ?

Why cu3(0,0,a) doesn't match with cu1(a) ?
Even if I ignore the global phase, these matrices seem different.
I have attached a simple code to check it.

Enviroment: Google Colab.

Get a matrix expression of cu1(a)

circ_test = Circuit(2)
circ_test.cu1(math.pi/4)[0,1]
Matrix = circ_test.to_unitary()
print(Matrix)

It Returns,
Matrix([[exp(-I*pi/16), 0, 0, 0], [0, exp(-I*pi/16), 0, 0], [0, 0, exp(-I*pi/16), 0], [0, 0, 0, exp(3*I*pi/16)]])

Get a matrix expression of cu3(0,0,a)

circ_test = Circuit(2)
circ_test.cu3(0,0,math.pi/4)[0,1]
Matrix = circ_test.to_unitary()
print(Matrix)

It Returns,
Matrix([[1, 0, 0, 0], [0, exp(-I*pi/8), 0, 0], [0, 0, 1, 0], [0, 0, 0, exp(I*pi/8)]])

Even if I ignore the global phase, these matrices seem different.
Is it expected behavior?


On the other hand, non-controlled version of these, namely, u3(0,0,a) and u1(a) , are same.
It is expected behavior.

Get a matrix expression of u1(a)

circ_test = Circuit(1)
circ_test.u1(math.pi/4)[0]
Matrix = circ_test.to_unitary()
print(Matrix)

Matrix([[exp(-I*pi/8), 0], [0, exp(I*pi/8)]])

Get a matrix expression of u3(0,0,a)

circ_test = Circuit(1)
circ_test.u3(0,0,math.pi/4)[0]
Matrix = circ_test.to_unitary()
print(Matrix)

Matrix([[exp(-I*pi/8), 0], [0, exp(I*pi/8)]])

[VQE] When Hamiltonian is constant, ValueError is raised

Code:

from blueqat.pauli import I
from blueqat import Circuit
from blueqat.vqe import *

h = 3 * I
runner = Vqe(QaoaAnsatz(h.to_expr()))
runner.run()

Error:

Traceback (most recent call last):
  File "bug_constant_hamiltonian.py", line 6, in <module>
    runner = Vqe(QaoaAnsatz(h.to_expr()))
  File "/home/penguin/blueqat/blueqat/vqe.py", line 64, in __init__
    super().__init__(hamiltonian, step * 2)
  File "/home/penguin/blueqat/blueqat/vqe.py", line 30, in __init__
    self.n_qubits = self.hamiltonian.max_n() + 1
  File "/home/penguin/blueqat/blueqat/pauli.py", line 709, in max_n
    return max(term.max_n() for term in self.terms if term.ops)
ValueError: max() arg is an empty sequence

KeyError of 'run_with_numpy'

I installed and executed the program of simple quantum circuit. But, keyerror occurs in circuit.py because of 'run_with_numpy'.

Executed program here.

-- coding: utf-8 --

"""
Created on Tue Oct 2 18:54:54 2018

@author: ****
"""

from blueqat import Circuit
import numpy as np
for i in range(100):
c = Circuit().x[3].x[3].x[0].rx(-np.pi/2)[0].ry(np.pi/2)[1]
c = c.cnot[1, 0].rz(np.pi/2)[0].cnot[1, 0].rx(np.pi/2)[0].ry(-np.pi/2)[1].m[:]
c.run()
print(c.last_result()) # => (1, 1)

Result here.
runfile('C:/Users/s1430/blueqatH2vqencitest1.py', wdir='C:/Users/s1430')
Reloaded modules: blueqat, blueqat.circuit, blueqat.gate, blueqat.backends.numpy_backend, blueqat.backends.backendbase, blueqat.backends.qasm_output_backend, blueqat.backends.mqc_backend, blueqat.pauli, blueqat.utils, blueqat.vqe
Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/s1430/blueqatH2vqencitest1.py', wdir='C:/Users/s1430')

File "C:\Users\s1430\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\s1430\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/s1430/blueqatH2vqencitest1.py", line 14, in
print(c.last_result()) # => (1, 1)

File "c:\users\s1430\downloads\blueqat-master_new\blueqat-master\blueqat\circuit.py", line 139, in last_result
return self._backends["run_with_numpy"].run_history[-1]

KeyError: 'run_with_numpy'

Statevector backend can't work well.

Statement

In recent blueqat updates, I guess that default backend is changed from a statevector to a sampler.
How can I use a statevector backend rather than a sampler in the latest blueqat?

Reproduce the problem

In old version of blueqat (probably, 2021),

import math
def print_Zbasis_expression(statevector):
    nqubit = int(math.log2(np.size(statevector)))
    for i in range(2**nqubit):
        print('+({:.2f})*|{number:0{width}b}>'.format(statevector[i],number=i,width=nqubit),end='')

State_out = Circuit(1).h[0].run()
print_Zbasis_expression(State_out)

returns,

+(0.71+0.00j)*|0>+(0.71+0.00j)*|1> 

However, in the latest blueqat (version 2.0.3), the same code returns a different result.

+(0.00)*|0>

I guess that default backend is changed from a statevector to a sampler.

Expected

Circuit(1).h[0].run(backend="statevector")
or
Circuit(1).h[0].run_with_ibmq(backend="statevector")
[0.71+0.00j, 0.71+0.00j]

Thanks.

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.