News

日本語 · English · 中文

[Contribution] I Tried Docking Superconducting Quantum Computer Circuits into the Hidden Layers of Generative AI Models

Construction: We built a hybrid model that incorporates quantum circuits into the hidden layers of generative AI (LLM) and performed simulator learning using blueqat. We report the results of attempting inference on the actual superconducting quantum computer "Toshiko" of OQC, using 1000 to 10,000 shots.

🤖 I Tried Docking a "Quantum Circuit" into the Hidden Layers of Generative AI

Hello everyone! This time, I’d like to introduce an exciting experiment that physically fuses AI and quantum computing.

We built a hybrid model by integrating a real "quantum computer" as a custom layer inside the network architecture of the main character, Generative AI (Large Language Model: LLM), and performing inference (text generation) via the actual device.


1. Mapping via "Rotation Gates" and Collecting "Expectation Values"

The quantum architecture used this time is the "gate model (quantum circuit model)," which constructs circuits by applying control inputs to qubits. We mapped the high-dimensional topology held by the LLM into the Hilbert space of quantum states.

Specifically, we connected the classical-quantum interface in series through the following steps:

  1. The mathematical vector of the hidden layer output from the LLM (TinyLlama) MLP layer is compressed once by a linear layer and normalized to the range of \(0 \sim \pi\) by applying activation (Sigmoid function). This is input as the rotation angle (parameter) for the "Pauli X rotation (\(Rx\) gate)" of the qubit.
  2. Inside the circuit, we forcibly generate strong "quantum entanglement" by sequentially applying \(CX\) (CNOT) gates between adjacent qubits, causing non-linear quantum interference that is difficult to imitate with classical computation.
  3. Finally, we tilt the basis with an \(Ry\) gate and perform measurement in the Z-basis. From sampling of 1000 shots (binary counting), we calculate the statistical "expectation value (probability vector)" of each qubit returning "1," and lift it back up via a linear layer to pass it to the next transformer block of the LLM.

This series of processes—"converting vectors to angles (rotation) to entangle them, and returning them to the classical domain as probabilities (expectation values)"—is embedded right at the heart of the generative AI's thinking process.


2. Simulation with the blueqat SDK and "Fine-Tuning" to Fit In

However, the initial state of the quantum circuit does not mesh at all with the LLM's internal representation (classical vectors); it is akin to having a completely incomprehensible foreign object inside the brain. At this stage, the model's Loss is very high, exceeding 12, and the output text is completely collapsed.

Since the output without any adjustment is essentially random:

Loading model 'TinyLlama/TinyLlama-1.1B-Chat-v1.0'...
Integrating quantum circuit into MLP of Layer 0...
Integration complete! Enter a prompt to test text generation.
--------------------------------------------------
[Generated Text]
Q: What is the capital of France?
A:cri Eineff logoA ausjaitter Faain a eariertoitter clas
--------------------------------------------------

It results in unintelligible strings. (Being able to infer at all is a success, though...)

Therefore, we first utilized the quantum SDK "blueqat," which allows for fast behavior simulation in a local environment, to perform "fine-tuning" to optimize the parameters of the classical linear layers before and after this quantum intermediate layer.

To ensure the PyTorch automatic differentiation graph was not interrupted by the processing of the independent quantum circuit, we inserted special tricks to execute gradient calculations and conducted training for 300 steps.

As a result of intensive training while gradually controlling the learning rate, the Avg Loss, which initially exceeded 12, drastically decreased to "0.66." This marks the moment when the LLM's peripheral networks harmonized with the characteristics of the expectation values output by the quantum circuit. We packed these optimized connection parameters locally as a 'Quantum Weight File (.pt).'

At this stage, even with the quantum circuit sandwiched in between, it is able to properly infer answers to questions. Since the sentences continue indefinitely, it might be possible to make it output just "Paris" cleanly with a bit more adjustment, but it has successfully reached the answer using the TinyLlama + Quantum Circuit hybrid (on the simulator).

--------------------------------------------------
Training complete! Post-tuning text generation test:
--------------------------------------------------
[Generated Text After 300 Steps of Training]
Q: What is the capital of France?
A: Paris. countryFrance. which AustriaWhat::
--------------------------------------------------

3. Carrying the Trained Weights to the Real Machine "Toshiko" Residing in Ariake, Tokyo!

Here lies the true highlight of this experiment. We load the "quantum weight file," perfectly trained in the simulator environment, directly into the real machine code, and finally connect to the genuine hardware.

The target is 'Toshiko,' a superconducting quantum computer (QPU) from Oxford Quantum Circuits (OQC), installed in a data center in Ariake, Tokyo.

First, as practice, we tried running it with random data. We inserted a quantum circuit into the middle of the LLM and executed it. At this state, quantum entanglement etc. are still half-baked, but let's see if it works at all.

Loading LLM model (TinyLlama)...
Initializing OQC client... (QPU ID: qpu:jp:3:673b1ad43c)

--- Starting real-machine hybrid inference on Toshiko (qpu:jp:3:673b1ad43c) with 1000 Shots ---
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots/binary_count). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.

[Quantum Real-Machine Hybrid LLM Generation Result]
Q: What is the capital of France?
A:gnugnugnu

Although the weights are random, the circuit isn't functioning as well as in the previous simulator, and perhaps there were too few shots, leading to poor character selection.

Interestingly, since we are submitting via API and the preparation to calculation completion is treated as a job, there is a slight delay for each character inference. Currently, it seems difficult to output characters quickly without some clever adjustments, such as placing things nearby.

However, now that we know inference is possible on the real machine, our next step is to try inserting the weights learned in the previous simulator. However, since the connections of the quantum circuit might change, further trials may be necessary from here on out.

It didn't work very well.

I will post the results below.

🤖 1. Loading LLM model (TinyLlama)...

--- ⏳ [Phase 1] Starting Simulator Training Conforming to Toshiko Linear Topology ---
   [Blueqat Training] Step 001/300 - Avg Loss: 9.1316
   [Blueqat Training] Step 002/300 - Avg Loss: 7.8450
   [Blueqat Training] Step 003/300 - Avg Loss: 6.9012
(Middle omitted)
   [Blueqat Training] Step 297/300 - Avg Loss: 0.3356
   [Blueqat Training] Step 298/300 - Avg Loss: 0.3356
   [Blueqat Training] Step 299/300 - Avg Loss: 0.3356
   [Blueqat Training] Step 300/300 - Avg Loss: 0.3356
💾 Saved training results (weight file): ./saved_models/quantum_middle_layer_weights.pt

--- 🛰️ [Phase 2] Transitioning to Real-Machine Hybrid Inference on OQC Toshiko in Ariake, Tokyo ---
Initializing OQC client... (QPU ID: qpu:jp:3:673b1ad43c)
📂 Loading the freshly saved quantum weight file...
✨ Successfully compiled and applied the simulator's training results to the real machine structure!

--- Real-time communication transmission to OQC qpu:jp:3:673b1ad43c (Toshiko) Ariake Data Center ---
Task sent to Toshiko real machine (1000shots). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
(Middle omitted)
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Task sent to Toshiko real machine (1000shots). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.

[🎉 Quantum Real-Machine Hybrid LLM Generation Result]
Q: Cats like
A:AA

So, the string "AA" appeared....

Next, we changed the problem...

[🎉 Quantum Real-Machine Hybrid LLM Generation Result]
Q: What is the capital of France?
A:
A

Still no good...

Possible Causes

Perhaps 1000 shots are insufficient???
If we pass the expectation value to the next layer, this small number of trials means we cannot deliver sufficient accuracy.

So, we tried it with 5000 shots.

🤖 1. Loading Generative AI model (TinyLlama)...

--- 🛰️ Building Real-Machine Hybrid Inference on OQC Toshiko in Ariake, Tokyo ---
Initializing OQC client... (QPU ID: qpu:jp:3:673b1ad43c)
📂 Found pre-saved quantum weight file. Loading...
✨ Successfully loaded training results adapted for Toshiko physical connection!

--- Sending real-time inference to OQC qpu:jp:3:673b1ad43c (Toshiko) Ariake Data Center (5000 shots) ---
Task sent to Toshiko real machine (5000shots). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
(Middle omitted)
Task sent to Toshiko real machine (5000shots). Task ID: 
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.
Real machine task status: SUBMITTED ... Will recheck in 2 seconds.

[🎉 Quantum Real-Machine Hybrid LLM Generation Result]
Q: What is the capital of France?
A: France:

!!!!!

It suddenly appeared. I really didn't think it would work so well...
I wanted it to say "Paris," but it said "France."
It’s somewhat satisfactory.

Getting carried away, we tried 10,000 shots.

[🎉 Quantum Real-Machine Hybrid LLM Generation Result]
Q: What is the capital of France?
A: France:

No change.
Finally, we repeated 10,000 shots about 14 times to complete the sentence.

There seem to be various areas for improvement, but we'll stop here for now.
Good work.

This article was translated from the Japanese original. 日本語で読む

← Latest articles