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

Implement parsing measure from text through ASG #42

Merged
merged 6 commits into from
Jan 19, 2024
Merged

Conversation

jlapeyre
Copy link
Collaborator

@jlapeyre jlapeyre commented Jan 17, 2024

Statements like c = measure q are analyzed correctly. But c[0] = measure q is not analyzed correctly. The code that builds the assignment statement assumes the LHS is an identifier. The ASG should be able to handle valid lvalues. But we cannot yet construct statements with them.

EDIT: This will address part of issue #38. But not all

This PR is intended to allow measure to work properly. But assignment to an indexed identifier is still broken. However, the latter is a more general issue and will be fixed in a future PR.

See the tests in "Files changed" to see examples of what has been fixed.

crates/oq3_semantics/src/asg.rs Outdated Show resolved Hide resolved
Comment on lines +575 to +585
// FIXME: type may not be correct here.
// This assumes a single qubit is measured.
pub fn to_texpr(self) -> TExpr {
TExpr::new(Expr::MeasureExpression(self), Type::Bit(IsConst::False))
}
Copy link
Member

Choose a reason for hiding this comment

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

I think the type map should be fairly straightforwards, if the program is semantically correct:

let out_type = match self.operand.get_type() {
    Type::Qubit => Ok(Type::Bit(IsConst::False)),
    Type::QubitArray(dims) => Ok(Type::BitArray(dims.clone(), IsConst::False)),
    _ => Err(<type error>),
}?;

crates/oq3_semantics/src/syntax_to_semantics.rs Outdated Show resolved Hide resolved
crates/oq3_semantics/src/syntax_to_semantics.rs Outdated Show resolved Hide resolved
Statements like `c = measure q` are analyzed correctly.
But `c[0] = measure q` is not analyzed correctly. The code
that builds the assignment statement assumes the LHS is an identifier.
The ASG should be able to handle valid lvalues. But we cannot yet
construct statements with them.
This is done twice and is several lines, so it is a function now.
This is unrelated to measurement.
@jlapeyre jlapeyre changed the title Partially implement parsing measure from text through ASG Implement parsing measure from text through ASG Jan 19, 2024
`GateOperand` is a qubit argument to gate call. And arguments to
`measure` have the same spec. Identifier, hardware identifier,
or indexed identifier. So we reuse the code from gate call.

The code in oq3_parser is a bit mysterious. I added some comments to
make it easier to locate next time, I hope.
@jlapeyre jlapeyre marked this pull request as ready for review January 19, 2024 00:48
@jlapeyre jlapeyre merged commit 32e84de into main Jan 19, 2024
4 checks passed
@jlapeyre jlapeyre deleted the measure-panic branch January 19, 2024 00:53
jlapeyre added a commit that referenced this pull request Jan 19, 2024
All of the failing examples given in #38 are included as (passing)
tests. Either in this PR or in earlier PRs.

Part of the fix for #38 was done in #42, which implemented measure.
The other part of the fix for #38 is in the present PR, which allows indexed identifiers as lvalues in assignment.

Closes #38
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