Skip to main content

Strangeworks VQE

The strangeworks-vqe package allows users to run construct and solve problems with the VQE algorithm on multiple hardware providers.

📑 Package Documentation

Overview​

The VQE algorithm allows you to solve quantum mechanical problems by making use of the power of quantum hardware. It is a classical-quantum hybrid algorithm: the general idea is that we propose a quantum ansatz circuit with some variational parameters as the coefficients for some of the quantum gates. We then apply this circuit to our qubits, measure the output and repeat many times in order to build up the required statistics. We take these measurement results and compute the cost function with our defined problem. We then hand the algorithm over to a classical CPU which optimizes the circuit parameters before passing back to the quantum side to re-measure the cost function but now for the new parameterized quantum circuit. The algorithm then iterates back and forth between the application of the quantum circuit and the classical optimization of the circuit parameters before eventually converging on a minimum energy.

This page will describe how to install and begin to use the VQE service. ​

Prerequisites​

In order to use the AWS Hybrid job service and/or the IBM Cloud devices, the user must have a billing account properly enabled. Without this, the user can still utilize qiskit runtime jobs through IBMs free services. ​

Installation​

To get started, make sure you have Python 3.10 or above (installation) and are familiar with setting up and using virtual environments.

Install packages using pip: ​

pip install -U pip && pip install strangeworks-vqe

​ Import package into python: ​

from strangeworks_vqe.sdk import StrangeworksVQE

​

Authentication​

Any issues authenticating, you may need to add a VQE resource to your account through the Platform, https://portal.strangeworks.com ​

Usage​

Before running:

  1. Set up your environment and install strangeworks-vqe and strangeworks
  2. In the portal, Activate Strangeworks VQE Service to create a Resource
  3. Replace your-api-key with your key from the Portal homepage
  4. Use examples below to utilize the various functionality. See Examples section below for an example script.

Authenticate with strangeworks python sdk using users API token:

strangeworks.authenticate(api_key="your-api-key")

​ Get VQE resource using vqe sdk extension and users resource slug:

resource = sw.get_resource_for_product('vqe')
sw_vqe = StrangeworksVQE(resource_slug=resource.slug)

​ List compatible backends for the vqe product:

sw_vqe.backends()

​ List all user vqe jobs:

jobs = sw_vqe.job_list(update_status=True)

​ Run problem:

sw_job = sw_vqe.run(backend, problem, problem_params)

​ Retrieve job with specific slug:

sw_job = strangeworks.jobs(slug=slug)[0]

​ Check the status of job and update

status = sw_vqe.update_status(sw_job)

Display results

result = sw_vqe.get_results(sw_job,calculate_exact_sol=True, display_results=True)

Examples​

IBM​

Here is an example run for a small problem on an IBM simulator:

import strangeworks
from strangeworks_vqe.sdk import StrangeworksVQE
import strangeworks_vqe.utils as utils

strangeworks.authenticate(api_key=" ", store_credentials=False)
sw_vqe = StrangeworksVQE(resource_slug=" ")

nqubits = 4
Jz = 0.5
Jxy = 1
H = utils.get_Heisenberg_PauliSumOp(nqubits, Jz, Jxy)

maxiter = 20
shotsin = 100
optimizer = "NELDER_MEAD"
ansatz = "RealAmplitudes"

problem_params = {
"maxiter": maxiter,
"shotsin": shotsin,
"nqubits": nqubits,
"optimizer": optimizer, # optional
"ansatz": ansatz, # optional
}

backend = "ibmq_qasm_simulator"
sw_job = sw_vqe.run(backend, H, problem_params)

result = sw_vqe.get_results(sw_job,calculate_exact_sol=True, display_results=True)

🥳 Success! You may view your job in the portal.

😅 Something went wrong? Find us in Slack!

AWS​

Here is an example run for a small problem on an AWS simulator:

import strangeworks
from strangeworks_vqe.sdk import StrangeworksVQE
import strangeworks_vqe.utils as utils

strangeworks.authenticate(api_key=" ", store_credentials=False)
sw_vqe = StrangeworksVQE(resource_slug=" ")

nqubits = 4
Jz = 0.5
Jxy = 1
H = utils.get_Heisenberg_PauliSumOp(nqubits, Jz, Jxy)

maxiter = 20
shotsin = 100
optimizer = "NELDER_MEAD"
ansatz = "RealAmplitudes"

problem_params = {
"maxiter": maxiter,
"shotsin": shotsin,
"nqubits": nqubits,
"optimizer": optimizer, # optional
"ansatz": ansatz, # optional
}

backend = "SV1"
sw_job = sw_vqe.run(backend, H, problem_params)

result = sw_vqe.get_results(sw_job,calculate_exact_sol=True, display_results=True)

Troubleshooting​

This section provides troubleshooting tips for the product or service this page covers. ​ ​ ​