Skip to content

Conversation

@yurekami
Copy link

Summary

  • Add support for converting Qiskit circuits with unbound parameters (ParameterExpression) to TorchQuantum
  • Previously, attempting to convert a circuit with symbolic parameters would fail with: TypeError: ParameterExpression with unbound parameters cannot be cast to a float.

Changes

  • Add _has_unbound_params() helper to detect unbound ParameterExpression objects
  • Add _convert_params() helper to safely convert gate parameters
  • Update qiskit2tq_op_history() to handle unbound parameters
  • Update qiskit2tq_Operator() to handle unbound parameters
  • Remove debug print statement from qiskit2tq_op_history()

Behavior

When a circuit has unbound parameters, the converted TorchQuantum layer will have trainable gates without initial parameter values (init_params=None), allowing them to be trained during optimization.

Example

from qiskit.circuit.library import RealAmplitudes
from torchquantum.plugin import qiskit2tq

# Previously failed with TypeError
ansatz = RealAmplitudes(2, reps=1)
tq_layer = qiskit2tq(ansatz)  # Now works!

Test plan

  • Convert RealAmplitudes circuit with unbound parameters
  • Convert circuit with ParameterVector
  • Verify bound parameters still work correctly
  • Verify mixed circuits (fixed + parameterized gates) work

Fixes #198

🤖 Generated with Claude Code

Add support for converting Qiskit circuits with unbound parameters
(ParameterExpression) to TorchQuantum. Previously, attempting to
convert a circuit with symbolic parameters would fail with:
    TypeError: ParameterExpression with unbound parameters cannot
    be cast to a float.

Changes:
- Add _has_unbound_params() helper to detect unbound parameters
- Add _convert_params() helper to safely convert gate parameters
- Update qiskit2tq_op_history() to handle unbound parameters
- Update qiskit2tq_Operator() to handle unbound parameters
- Remove debug print statement from qiskit2tq_op_history()

When a circuit has unbound parameters, the converted TorchQuantum
layer will have trainable gates without initial parameter values,
allowing them to be trained during optimization.

Fixes mit-han-lab#198

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
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.

TypeError when using Qiskit plugin to convert circuit with a ParameterExpression

1 participant