Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Support pennylane's Hamiltonian and Circuit #107

Merged
merged 32 commits into from
Jan 24, 2025

Conversation

ahao27
Copy link
Contributor

@ahao27 ahao27 commented Nov 28, 2024

Description

Add a new transpiler to support Pennylane's Hamiltonian format in Qamomile.
This PR introduces a transpiler specifically designed to convert Qamomile's Hamiltonians into Pennylane's Hamiltonians, enhancing the interoperability between the two quantum frameworks.

Changes

  1. Added Core Transpiler Implementation:

Implements the PennylaneTranspiler class, which handles the conversion of Qamomile's Hamiltonians into Pennylane's qml.Hamiltonian objects.

  • transpiler.py:
    Supports Pauli-X, Pauli-Y, and Pauli-Z operators, with appropriate error handling for unsupported cases.

  • exception.py:
    Introduces custom exceptions for handling unsupported operations during the transpilation process.

  • init.py:
    Allows for module initialization and ensures proper package integration.

  1. Unit Tests:

Created test_pennylane_transpiler.py to validate the correctness of the transpiler.
Test cases cover:

  • Basic Hamiltonians with single terms (e.g., X @ Z).
  • Complex Hamiltonians with multiple terms (e.g., X @ Y + Z @ X).
  1. Docstring:

Inline docstrings were added to describe the purpose and functionality of each method and class.

Copy link
Collaborator

@Jacomichi Jacomichi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also fix pyproject.toml because the tests fail

Qamomile/pyproject.toml

Lines 45 to 47 in b1efe3c

qiskit = ["qiskit"]
quri-parts = ["quri-parts-qulacs"]
qutip = ["qutip"]

@ahao27
Copy link
Contributor Author

ahao27 commented Nov 28, 2024

Changes

  1. In Qamomile/pyproject.toml, the pennylane = ["pennylane"] is added.
  2. In Qamomile/pyproject.toml, the pennylane = "^0.39.0" is added.

@ahao27 ahao27 closed this Nov 28, 2024
@ahao27 ahao27 reopened this Nov 28, 2024
@Jacomichi Jacomichi self-requested a review November 28, 2024 05:20
Copy link
Collaborator

@Jacomichi Jacomichi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left the comments.

pyproject.toml Outdated
@@ -25,7 +25,7 @@ quri-parts-circuit = "^0.19.0"
qiskit = "^1.1.1"
matplotlib = "^3.9.2"
qutip = "^5.0.4"

PennyLane = "^0.39.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small letter is better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIxed

pyproject.toml Outdated
@@ -45,6 +45,7 @@ matplotlib = "^3.9.2"
qiskit = ["qiskit"]
quri-parts = ["quri-parts-qulacs"]
qutip = ["qutip"]
PennyLane = ["PennyLane"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small letter is better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

yuyamashiro
yuyamashiro previously approved these changes Dec 2, 2024
@Jacomichi Jacomichi self-requested a review December 3, 2024 01:58
Jacomichi
Jacomichi previously approved these changes Dec 3, 2024
@ahao27 ahao27 requested a review from Jacomichi December 5, 2024 08:55
@ahao27
Copy link
Contributor Author

ahao27 commented Dec 5, 2024

Here is a simple usage of the Parameter gate transfer from QamomileCircuit to pennylane Qnode.

from qamomile.core.circuit import QuantumCircuit as QamomileCircuit
import pennylane as qml
import numpy as np
import qamomile
from qamomile.pennylane.transpiler import PennylaneTranspiler
from qamomile.core.circuit import Parameter

qc = QamomileCircuit(3)
theta = Parameter("theta")
beta = Parameter("beta")
gamma = Parameter("gamma")

qc.rx(theta, 0)
qc.ry(beta, 1)
qc.rz(gamma, 2)
qc.crx(gamma, 0 ,1)
qc.crz(theta, 1 ,2)
qc.cry(beta, 2 ,0)

transpiler = PennylaneTranspiler()
QNode = transpiler.transpile_circuit(qc)
print("QN: ", QNode)
p= {"theta":0.1,"beta":0.2, "gamma": 0.3}
qml.drawer.use_style("black_white")
fig, ax = qml.draw_mpl(QNode)(theta=0.1,beta=0.2, gamma=0.3)

@ahao27 ahao27 changed the title Feature/Support pennylane's Hamiltonian Feature/Support pennylane's Hamiltonian and Circuit Dec 6, 2024
@ahao27 ahao27 dismissed stale reviews from Jacomichi and yuyamashiro December 6, 2024 01:18

New feture updated

@Jacomichi Jacomichi self-requested a review December 20, 2024 03:57
@yuyamashiro
Copy link
Contributor

yuyamashiro commented Jan 17, 2025

Fixed _extract_variable so that it can evaluate any expression that can be constructed by traversing ParameterExpression.

@ahao27
The original implementation could only evaluate linear cases, but was that intended? Please check that your current implementation matches the original intent.

def _extract_angle(
self,
gate: qamomile.core.circuit.ParametricSingleQubitGate | qamomile.core.circuit.ParametricTwoQubitGate,
params
):
"""
Extract the angle parameter for parameterized gates from the params dictionary.
Args:
gate: A parameterized gate (single or two-qubit).
params: A dictionary mapping parameter names to their numeric values.
Returns:
float: The numeric angle/value for the gate.
"""
return qamomile.core.circuit.parameter.substitute_param_expr(
gate.parameter, params
)

@yuyamashiro
Copy link
Contributor

I have modified _create_param_mapping. Since gate.parameters returns list[Parameter], I simplified the implementation.

def _create_param_mapping(
self, qamomile_circuit: qamomile.core.circuit.QuantumCircuit
) -> Dict[str, qamomile.core.circuit.Parameter]:
"""
Create a parameter mapping dictionary from parameter names to Qamomile parameters.
Args:
qamomile_circuit (qamomile.core.circuit.QuantumCircuit): The circuit containing parameters.
Returns:
Dict[str, qamomile.core.circuit.Parameter]: A mapping from parameter names to parameters.
"""
parameters = qamomile_circuit.get_parameters()
param_mapping = {param.name: param for param in parameters}

Copy link
Collaborator

@Jacomichi Jacomichi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please implement convert_result

"source": [
"### Pennylane Backend\n",
"\n",
"Here's how you can execute a Qamomile circuit using Qpennylane.\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qpennylane is typo

qamomile/pennylane/transpiler.py Outdated Show resolved Hide resolved
@Jacomichi Jacomichi self-requested a review January 22, 2025 00:27
Copy link
Collaborator

@Jacomichi Jacomichi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I have left the comments

"""
Convert the result from PennyLane execution to a Qamomile-compatible result.

Currently not implemented.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write the docstring?
And also, can you fix the type?

@Jacomichi Jacomichi self-requested a review January 23, 2025 10:45
return None

decimal_result_data = {binary_to_decimal(key): value for key, value in result.items()}
num_bits = len(result.popitem()[0])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think popitem modifies the result, so we should not use it.

@Jacomichi Jacomichi self-requested a review January 23, 2025 16:34
Copy link
Collaborator

@Jacomichi Jacomichi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@yuyamashiro
Copy link
Contributor

👍

@yuyamashiro yuyamashiro merged commit b0952de into main Jan 24, 2025
6 checks passed
@yuyamashiro yuyamashiro deleted the feature/PennylaneTranpiler branch January 24, 2025 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants