Skip to main content

Module expr

Module expr 

Source
Expand description

Typed, side-effect-free runtime classical expressions.

Expressions read ClassicalVar and ClassicalValue handles and combine them into typed runtime values. They do not measure qubits, write classical storage, or transfer control. Control-flow statements consume these expressions in a later layer.

Build expressions with the typed constructors on ClassicalExpr. ClassicalVar and ClassicalValue handles can be converted directly into expressions, so var.expr(), ClassicalExpr::from(var), and passing a handle to a try_* builder are equivalent expression sources. Leaf constructors such as ClassicalExpr::bool_literal and ClassicalExpr::bit_literal are infallible. Constructors that validate operand types return Result<ClassicalExpr, CircuitError>; the fallible logical builders use a try_ prefix, for example ClassicalExpr::try_not, ClassicalExpr::try_and, ClassicalExpr::try_or, and ClassicalExpr::try_xor.

Prefer Rust bit operators when the expression types are already known: !source, lhs & rhs, lhs | rhs, and lhs ^ rhs. Each source may be a ClassicalExpr, ClassicalVar, or ClassicalValue. These operator overloads panic if the operands are not matching Bool or matching Bit expressions, because the standard operator traits cannot return Result. Use the fallible try_* builders when invalid input should be reported as CircuitError instead of a panic.

use cqlib_core::circuit::{CircuitId, ClassicalExpr, ClassicalType, ClassicalVar};

let cid = CircuitId::new();
let a = ClassicalVar::new(cid, 0, ClassicalType::Bool);
let b = ClassicalVar::new(cid, 1, ClassicalType::Bool);

let condition = !a & b;
assert_eq!(condition.ty(), ClassicalType::Bool);

let checked = ClassicalExpr::try_and(ClassicalExpr::try_not(a)?, b)?;
assert_eq!(checked.ty(), ClassicalType::Bool);

Structs§

ClassicalExpr
A typed, side-effect-free runtime classical expression.
ClassicalExprNode
Typed node stored inside a ClassicalExpr.

Enums§

ClassicalBinaryOp
Binary operators for boolean and bit expressions.
ClassicalCast
Explicit casts between classical expression types.
ClassicalCompareOp
Comparison operators for classical expressions.
ClassicalExprKind
Public node kind for inspecting a ClassicalExpr AST.
ClassicalUnaryOp
Unary operators for classical expressions.