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

refactor: Simplify some internals of qiskit_to_tk conversion #382

Merged
merged 26 commits into from
Sep 10, 2024

Conversation

CalMacCQ
Copy link
Contributor

@CalMacCQ CalMacCQ commented Aug 24, 2024

Description

This PR begins a refactor of the qiskit_to_tk and tk_to_qiskit converters. Here I've focussed on refactoring some of the large CircuitBuilder.add_qiskit_data method (used as part of qiskit_to_tk into separate internal functions. I've added some short docstrings so its hopefully clearer whats going on.

There's still a lot to improve in the conversion code but I think it makes sense to do this refactor in several stages to make small fixes like #366 a bit easier.

I've avoided imporvements which involve user facing changes for now. Will add these in a follow up.

Related issues

#313

Checklist

  • I have performed a self-review of my code.
  • I have commented hard-to-understand parts of my code.
  • I have made corresponding changes to the public API documentation.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have updated the changelog with any user-facing changes.

@CalMacCQ CalMacCQ requested a review from cqc-melf as a code owner August 24, 2024 00:43
@CalMacCQ CalMacCQ marked this pull request as draft August 24, 2024 00:44
@CalMacCQ CalMacCQ changed the title refactor: qiskit circuit converter refactor: qiskit_to_tk and tk_to_qiskit Aug 24, 2024
@CalMacCQ
Copy link
Contributor Author

CalMacCQ commented Aug 27, 2024

In fact I think its better to do the type annotations updates for the whole repo in a separate PR. Clutters the diff quite a bit.

See #383

@CalMacCQ CalMacCQ changed the title refactor: qiskit_to_tk and tk_to_qiskit refactor: Simplify internals of qiskit_to_tk conversion Sep 9, 2024
@CalMacCQ CalMacCQ changed the title refactor: Simplify internals of qiskit_to_tk conversion refactor: Simplify some internals of qiskit_to_tk conversion Sep 9, 2024
@CalMacCQ CalMacCQ requested a review from cqc-alec September 9, 2024 16:07
@CalMacCQ CalMacCQ marked this pull request as ready for review September 9, 2024 16:08
tkc.add_circuit(circuit, qubits)
else:
raise TypeError(
"Unrecognised type of Instruction.params "
Copy link
Contributor Author

@CalMacCQ CalMacCQ Sep 9, 2024

Choose a reason for hiding this comment

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

I'm not so satisfied with this _add_state_preparataion function. There are at least 3 different ways to create a StatePreparation or Initialize object. Couldn't simplify the handling all that much.

Copy link
Collaborator

@cqc-alec cqc-alec left a comment

Choose a reason for hiding this comment

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

Thanks, just a couple of minor suggestions.

Comment on lines 298 to 299
known_optype = _known_qiskit_gate[c_gate.base_class]
return known_optype
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
known_optype = _known_qiskit_gate[c_gate.base_class]
return known_optype
return _known_qiskit_gate[c_gate.base_class]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 37de5e4

Comment on lines 362 to 369
else:
if isinstance(prep.params[0], complex):
# convert int to a binary string and apply X for |1>
integer_parameter = int(prep.params[0].real)
bit_string = bin(integer_parameter)[2:]
circuit = _string_to_circuit(bit_string, prep.num_qubits, qiskit_prep=prep)
tkc.add_circuit(circuit, qubits)
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use elif and else here to save indentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done in 37de5e4

@CalMacCQ CalMacCQ requested a review from cqc-alec September 10, 2024 08:46
@CalMacCQ CalMacCQ merged commit dc2357d into main Sep 10, 2024
6 checks passed
@CalMacCQ CalMacCQ deleted the refactor/qiskit_converter branch October 24, 2024 21:57
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.

2 participants