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, problem='MaxCut', solutions=3)
    

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).

  • BAQIS Quafu Group, "Quafu-Qcover: Explore Combinatorial Optimization Problems on Cloud-based Quantum Computers", arXiv:2305.17979 (2023).

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.6.0.tar.gz (56.5 kB view details)

Uploaded Source

Built Distribution

Qcover-2.6.0-py3-none-any.whl (92.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: Qcover-2.6.0.tar.gz
  • Upload date:
  • Size: 56.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for Qcover-2.6.0.tar.gz
Algorithm Hash digest
SHA256 6bb2c19001fef95c4d4739abd8ae14988648bb7b604a7d77750f640bc8daa87a
MD5 62119452bd6ad30f0ccb44518107c440
BLAKE2b-256 257e76da3f459f8b93b68ee3b00dab1a08a39588687a63ed6e672ac375d2a0f5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Qcover-2.6.0-py3-none-any.whl
  • Upload date:
  • Size: 92.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for Qcover-2.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 101d46dae30a24ee662e4c7bb932323e6e9c0a5caa25e268334b1634b3ea3614
MD5 b0e2611512f619a79762056d9e8e877c
BLAKE2b-256 e7e5a370423f99788a224296d0aafbdef5bb86db87cab43e69c3a73238e3b310

See more details on using hashes here.

Supported by

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