Skip to main content

Quantum computing solver

Project description

Qcover is an open source effort to help exploring combinatorial optimization problems in Noisy Intermediate-scale Quantum(NISQ) processor. It is developed by the quantum operating system team in Beijing Academy of Quantum Information Sciences (BAQIS). Qcover supports fast output of optimal parameters in shallow QAOA circuits. It can be used as a powerful tool to assist NISQ processor to demonstrate application-level quantum advantages.

Getting started

Use the following command to complete the installation of Qcover

pip install Qcover

or

git clone https://github.com/BAQIS-Quantum/Qcover
pip install -r requirements.yml
python setup.py install

More example codes and tutorials can be found in the tests folder here on GitHub.

Examples

  1. Using algorithm core module to generate the ising random weighted graph and calculate it's Hamiltonian expectation

    from Qcover.core import Qcover
    from Qcover.backends import CircuitByQulacs
    from Qcover.optimizers import COBYLA
    from Qcover.utils import generate_graph_data, generate_weighted_graph
    import networkx as nx
    
    node_num, edge_num = 6, 9
    p = 1
    nodes, edges = generate_graph_data(node_num, edge_num)
    g = generate_weighted_graph(nodes, edges)
    
    # If you want to customize the Ising weight graph model, you can use the following code
    # g = nx.Graph()
    # nodes = [(0, 3), (1, 2), (2, 1), (3, 1)]
    # edges = [(0, 1, 1), (0, 2, 1), (3, 1, 2), (2, 3, 3)]
    # for nd in nodes:
    #    u, w = nd[0], nd[1]
    #    g.add_node(int(u), weight=int(w))
    # for ed in edges:
    #     u, v, w = ed[0], ed[1], ed[2]
    #     g.add_edge(int(u), int(v), weight=int(w))
    
    qulacs_bc = CircuitByQulacs()
    optc = COBYLA(options={'tol': 1e-3, 'disp': True})
    qc = Qcover(g, p=p, optimizer=optc, backend=qulacs_bc)
    res = qc.run()
    print("the result of problem is:\n", res)
    qc.backend.optimization_visualization()
    
  2. Solving specific binary combinatorial optimization problems, Calculating the expectation value of the Hamiltonian of the circuit which corresponding to the problem. for example, if you want to using Qcover to solve a max-cut problem, just coding below:

    from Qcover.core import Qcover
    from Qcover.backends import CircuitByQiskit
    from Qcover.optimizers import COBYLA
    from Qcover.applications.max_cut import MaxCut
    
    node_num, degree = 6, 3
    p = 1
    mxt = MaxCut(node_num=node_num, node_degree=degree)
    ising_g, shift = mxt.run()
    qiskit_bc = CircuitByQiskit(expectation_calc_method="statevector")
    optc = COBYLA(options={'tol': 1e-3, 'disp': True})
    qc = Qcover(ising_g, p=p, optimizer=optc, backend=qiskit_bc)
    res = qc.run()
    print("the result of problem is:\n", res)
    counts = qc.backend.get_result_counts(res['Optimal parameter value'])
    qc.backend.sampling_visualization(counts)
    
  3. If you want to solve combinatorial optimization problems with real quantum computers on quafu quantum computing cloud platform, you can refer to the following example code

    from Qcover.core import Qcover
    from Qcover.backends import CircuitByQulacs
    from Qcover.optimizers import COBYLA
    from Qcover.compiler import CompilerForQAOA
    import networkx as nx
    import matplotlib.pyplot as plt
    
    # Qcover supports real quantum computers to solve combinatorial optimization problems.
    # You only need to transform the combinatorial optimization problem into a weight graph,
    # and you can use the quafu quantum computing cloud platform  (http://quafu.baqis.ac.cn/)
    # to solve the corresponding problem. The following is an example of a max-cut problem.
    
    # The weight graph corresponding to the combinatorial optimization problem and transformed it to networkx format.
    nodes = [(0, 1), (1, 3), (2, 2), (3, 1), (4, 0), (5, 3)]
    edges = [(0, 1, -1), (1, 2, -4), (2, 3, 2), (3, 4, -2), (4, 5, -1), (1, 3, 0), (2, 4, 3)]
    graph = nx.Graph()
    for nd in nodes:
        u, w = nd[0], nd[1]
        graph.add_node(int(u), weight=int(w))
    for ed in edges:
        u, v, w = ed[0], ed[1], ed[2]
        graph.add_edge(int(u), int(v), weight=int(w))
    
    # draw weighted graph to be calculated
    new_labels = dict(map(lambda x: ((x[0], x[1]), str(x[2]['weight'])), graph.edges(data=True)))
    pos = nx.spring_layout(graph)
    # pos = nx.circular_layout(g)
    nx.draw_networkx(graph, pos=pos, node_size=400, font_size=13, node_color='y')
    # nx.draw_networkx_edge_labels(g, pos=pos, edge_labels=new_labels, font_size=15)
    nx.draw_networkx_edges(graph, pos, width=2, edge_color='g', arrows=False)
    plt.show()
    
    # Using Qcover to calculate the optimal parameters of QAOA circuit.
    p = 1
    bc = CircuitByQulacs()
    optc = COBYLA(options={'tol': 1e-3, 'disp': True})
    qc = Qcover(graph, p=p, optimizer=optc, backend=bc)
    res = qc.run()
    optimal_params = res['Optimal parameter value']
    
    # Compile and send the QAOA circuit to the quafu cloud.
    # Token parameter should be set according to your own account
    # For more introduction see https://github.com/ScQ-Cloud/pyquafu
    token = "E-SowFQdKJ427YhZDGdxoNmOk2SB02xpgODiz_4WtAS.9dDOwUTNxgjN2EjOiAHelJCLzITM6ICZpJye.9JiN1IzUIJiOicGbhJCLiQ1VKJiOiAXe0Jye"
    cloud_backend = 'ScQ-P20'
    qcover_compiler = CompilerForQAOA(graph, p=p, optimal_params=optimal_params, apitoken=token, cloud_backend=cloud_backend)
    task_id = qcover_compiler.send(wait=True, shots=5000, task_name='MaxCut')
    # If you choose wait=Ture, you have to wait for the result to return.
    # If you choose wait=False, you can execute the following command to query the result status at any time,
    # and the result will be returned when the task is completed.
    quafu_solver = qcover_compiler.task_status_query(task_id)
    if quafu_solver:
        counts_energy = qcover_compiler.results_processing(quafu_solver)
        qcover_compiler.visualization(counts_energy)
    

The results obtained by running this example code are shown in the following two figures

How to contribute

For information on how to contribute, please send an e-mail to members of developer of this project.

Please cite

When using Qcover for research projects, please cite

  • Wei-Feng Zhuang, Ya-Nan Pu, Hong-Ze Xu, Xudan Chai, Yanwu Gu, Yunheng Ma, Shahid Qamar, Chen Qian, Peng Qian, Xiao Xiao, Meng-Jun Hu, and Done E. Liu, "Efficient Classical Computation of Quantum Mean Value for Shallow QAOA Circuits", arXiv:2112.11151 (2021).

Authors

The first release of Qcover was developed by the quantum operating system team in Beijing Academy of Quantum Information Sciences.

Qcover is constantly growing and many other people have already contributed to it in the meantime.

License

Qcover is released under the Apache 2 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Qcover-2.4.0.tar.gz (57.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Qcover-2.4.0-py3-none-any.whl (92.0 kB view details)

Uploaded Python 3

File details

Details for the file Qcover-2.4.0.tar.gz.

File metadata

  • Download URL: Qcover-2.4.0.tar.gz
  • Upload date:
  • Size: 57.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for Qcover-2.4.0.tar.gz
Algorithm Hash digest
SHA256 1bd1e948fdc31281f574d86e69a6a450fb42219a58ab6d03b9899684e1f65030
MD5 9981347f025b60693e9388c85bb8e9a5
BLAKE2b-256 f37b931d871bdbd82915032806bec7294606d9c054a375aa3ac4c45c1be4033f

See more details on using hashes here.

File details

Details for the file Qcover-2.4.0-py3-none-any.whl.

File metadata

  • Download URL: Qcover-2.4.0-py3-none-any.whl
  • Upload date:
  • Size: 92.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for Qcover-2.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f61416cfe7c2aa9eb29eb35ea29aacb5f4baa0effac7dbc1949908c75b3d1809
MD5 c1c3506b3a194864b31fec0138b1ab5e
BLAKE2b-256 3d730306ee65aa26eb9153ea71cd7050a11e77bbf392b292a48f4cfefe8e7b26

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page