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

Improve typing in phirgen, fix measure args bug, pretty print PHIR #13

Merged
merged 5 commits into from
Oct 19, 2023

Conversation

qartik
Copy link
Member

@qartik qartik commented Oct 18, 2023

Sample outputs:

# pytest tests/test_api.py::TestApi::test_pytket_to_phir_h1_1
PHIRModel(
    format_='PHIR/JSON',
    version='0.1.0',
    metadata={'source': 'pytket-phir'},
    ops=[
        QVarDefine(metadata=None, data='qvar_define', data_type='qubits', variable='q', size=2),
        CVarDefine(metadata=None, data='cvar_define', data_type='i64', variable='c', size=2),
        Comment(c='Rz(1) q[0];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='Rz', returns=None, args=[['q', 0]], angles=[1.0]),
        Comment(c='PhasedX(0.5, 0.5) q[0];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='PhasedX', returns=None, args=[['q', 0]], angles=[0.5, 0.5]),
        Comment(c='Rz(1) q[0];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='Rz', returns=None, args=[['q', 0]], angles=[1.0]),
        Comment(c='Rz(1) q[1];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='Rz', returns=None, args=[['q', 1]], angles=[1.0]),
        Comment(c='PhasedX(0.5, 0.5) q[1];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='PhasedX', returns=None, args=[['q', 1]], angles=[0.5, 0.5]),
        Comment(c='Rz(1) q[1];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='Rz', returns=None, args=[['q', 1]], angles=[1.0]),
        Comment(c='PhasedX(1.5, 0.5) q[1];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='PhasedX', returns=None, args=[['q', 1]], angles=[1.5, 0.5]),
        Comment(c='ZZPhase(0.5) q[0], q[1];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='ZZPhase', returns=None, args=[['q', 0], ['q', 1]], angles=[0.5]),
        MOp(metadata={'duration': 0.0}, mop='Transport'),
        Comment(c='Rz(1.5) q[0];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='Rz', returns=None, args=[['q', 0]], angles=[1.5]),
        Comment(c='Measure q[0] --> c[0];'),
        QOp(metadata=None, qop='Measure', returns=[['c', 0]], args=[['q', 0]], angles=None),
        Comment(c='Rz(0.5) q[1];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='Rz', returns=None, args=[['q', 1]], angles=[0.5]),
        Comment(c='PhasedX(1.5, 0.5) q[1];'),
        QOp(metadata={'angle_multiplier': 'π'}, qop='PhasedX', returns=None, args=[['q', 1]], angles=[1.5, 0.5]),
        Comment(c='Measure q[1] --> c[1];'),
        QOp(metadata=None, qop='Measure', returns=[['c', 1]], args=[['q', 1]], angles=None),
        MOp(metadata={'duration': 0.0}, mop='Transport')
    ]
)
# python tests/e2e_test.py
PHIRModel(
    format_='PHIR/JSON',
    version='0.1.0',
    metadata={'source': 'pytket-phir'},
    ops=[
        QVarDefine(metadata=None, data='qvar_define', data_type='qubits', variable='q', size=3),
        CVarDefine(metadata=None, data='cvar_define', data_type='i64', variable='c', size=3),
        Comment(c='CX q[0], q[2];'),
        QOp(metadata=None, qop='CX', returns=None, args=[['q', 0], ['q', 2]], angles=None),
        Comment(c='H q[1];'),
        QOp(metadata=None, qop='H', returns=None, args=[['q', 1]], angles=None),
        Comment(c='Measure q[1] --> c[1];'),
        QOp(metadata=None, qop='Measure', returns=[['c', 1]], args=[['q', 1]], angles=None),
        MOp(metadata={'duration': 2e-06}, mop='Transport'),
        Comment(c='H q[0];'),
        QOp(metadata=None, qop='H', returns=None, args=[['q', 0]], angles=None),
        Comment(c='Measure q[0] --> c[0];'),
        QOp(metadata=None, qop='Measure', returns=[['c', 0]], args=[['q', 0]], angles=None),
        Comment(c='H q[2];'),
        QOp(metadata=None, qop='H', returns=None, args=[['q', 2]], angles=None),
        Comment(c='Measure q[2] --> c[2];'),
        QOp(metadata=None, qop='Measure', returns=[['c', 2]], args=[['q', 2]], angles=None),
        MOp(metadata={'duration': 0.0}, mop='Transport')
    ]
)

@qartik qartik changed the title Improve typing in phirgen (allow Any) Improve typing in phirgen, fix measure args bug, pretty print PHIR Oct 18, 2023
@qartik qartik self-assigned this Oct 18, 2023
@qartik qartik added the bug Something isn't working label Oct 18, 2023
"metadata": metadata,
"angles": angles,
"qop": gate,
"args": [],
}
for qbit in cmd.args:
qop["args"].append([qbit.reg_name, qbit.index[0]])
if gate == "Measure":
break
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously, both the qbit and cbit were part of the args for Measure.

@qartik qartik requested a review from qciaran October 19, 2023 02:33
@@ -47,12 +42,12 @@ def genphir(inp: list[tuple[list[int], list[Shard], float]]) -> str:
Args:
inp: list of shards
"""
phir = {
phir: dict[str, Any] = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

No better types we can give than Any?

Copy link
Member Author

Choose a reason for hiding this comment

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

With the current approach, i.e., constructing Python dict's instead of a PHIRModel object, the best we can do is Any. Someday, we can consider directly constructing PHIRModel but I found it difficult to go that route in my first attempt.

Copy link
Collaborator

@peter-campora peter-campora left a comment

Choose a reason for hiding this comment

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

Looks good. My only comment was about whether we could replace Any with a different annotation, but we can make changes like that gradually.

@qartik qartik merged commit 4e963f1 into main Oct 19, 2023
1 check passed
@qartik qartik deleted the phirgen-typing branch October 19, 2023 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants