Python SDK
This is the Python-SDK for using the Q-Alchemy API which helps quantum computing researchers to put classical data into the quantum computer. This is all also called: the loading problem, encoding problem, or quantum state preparation. Some people also call it a form of QRAM, or quantum random-access memory.
This SDK builds upon the Hypermedia-Siren API of data cybernetics which uses a document-first approach added with actions. The standardized way makes the API programmatically accessible, which can be explored by the Hypermedia-Test-UI
The SDK builds upon this, so that any software developer planning to integrate with the API and experience the API through the UI and the SDK in a very similar fashion. Also, any GUI around this has similar characteristics.
pip install q-alchemy-sdk-pypdm add q-alchemy-sdk-pyuv pip install q-alchemy-sdk-pyThere are examples under the /examples folder, but for those that are eager to find out, here it is. First, you will want to get an API key from the Q-Alchemy Portal. You need to sign up for this, sorry, but this is necessary. Once you have the API key (free of charge of course) you can test it!
Direct Example
Section titled “Direct Example”import numpy as npfrom sklearn.datasets import fetch_openml
from q_alchemy.initialize import q_alchemy_as_qasm
mnist = fetch_openml('mnist_784', version=1, parser="auto")
zero: np.ndarray = mnist.data[mnist.target == "0"].iloc[0].to_numpy()filler = np.empty(2 ** 10 - zero.shape[0])filler.fill(0)
zero = np.hstack([zero, filler])zero = zero / np.linalg.norm(zero)
qasm, summary = q_alchemy_as_qasm(zero, max_fidelity_loss=0.2, api_key="<your api key>", return_summary=True)print(summary)Qiskit Example
Section titled “Qiskit Example”import numpy as npfrom sklearn.datasets import fetch_openml
from q_alchemy.qiskit_integration import QAlchemyInitialize, OptParams
mnist = fetch_openml('mnist_784', version=1, parser="auto")
zero: np.ndarray = mnist.data[mnist.target == "0"].iloc[0].to_numpy()filler = np.empty(2 ** 10 - zero.shape[0])filler.fill(0)
zero = np.hstack([zero, filler])zero = zero / np.linalg.norm(zero)
instr = QAlchemyInitialize( params=zero.tolist(), opt_params=OptParams( max_fidelity_loss=0.1, basis_gates=["id", "rx", "ry", "rz", "cx"], api_key="<your api key>" ))instr.definition.draw(fold=-1)PennyLane Example
Section titled “PennyLane Example”import numpy as npimport pennylane as qmlfrom sklearn.datasets import fetch_openml
from q_alchemy.pennylane_integration import QAlchemyStatePreparation, OptParams
mnist = fetch_openml('mnist_784', version=1, parser="auto")
zero: np.ndarray = mnist.data[mnist.target == "0"].iloc[0].to_numpy()filler = np.empty(2 ** 10 - zero.shape[0])filler.fill(0)
zero = np.hstack([zero, filler])zero = zero / np.linalg.norm(zero)
dev = qml.device('lightning.qubit', wires=10)
@qml.qnode(dev)def circuit(state=None): QAlchemyStatePreparation( state, wires=range(10), opt_params=OptParams( max_fidelity_loss=0.1, basis_gates=["id", "rx", "ry", "rz", "cx"], api_key="<your api key>" ) ) return qml.state()
print(qml.draw(circuit, level="device", max_length=100)(zero.tolist()))Broadcasting with PennyLane
Section titled “Broadcasting with PennyLane”PennyLane provides native support for broadcasting, which allows quantum nodes to process batches of inputs efficiently. This is particularly useful in machine learning applications where inputs often come in batches. When broadcasting is used in conjunction with Q-Alchemy, each state in the batch is individually prepared using Q-Alchemy’s circuit synthesis capabilities.
Broadcasting Example with qiskit.aer
Section titled “Broadcasting Example with qiskit.aer”import numpy as npimport pennylane as qmlimport torch
from q_alchemy.pennylane_integration import AmplitudeEmbedding, OptParamsfrom sklearn.datasets import make_moons
# Sample dataX, _ = make_moons(n_samples=5, noise=0.1)X = X / np.linalg.norm(X, axis=1, keepdims=True) # Normalize each row for amplitude embedding
# Create PennyLane devicedev = qml.device("qiskit.aer", wires=1)
@qml.qnode(dev, interface="torch")def circuit(x): AmplitudeEmbedding( x, wires=[0], opt_params=OptParams( max_fidelity_loss=0.0, api_key="<your api key>" ) ) return qml.expval(qml.PauliZ(0))
# Run the circuit on a batch of inputsX_tensor = torch.tensor(X, dtype=torch.float64)print(qml.draw(circuit, level="device", max_length=100)(X_tensor))This example demonstrates how batched data can be processed using broadcasting with AmplitudeEmbedding, and how Q-Alchemy is triggered on simulators like qiskit.aer. When moving to real hardware or gate-based backends that lack StatePrep gate, Q-Alchemy will transparently handle the state preparation.
License
Section titled “License”The q-alchemy-sdk-py is free and open source, released under the Apache License, Version 2.0.