uniqc.circuit_builder.classical_program module¶
Classical / control-flow program tree for OriginIR-ext.
This module extends the flat opcode_list circuit representation with a
structured program tree expressing mid-circuit measurement, a runtime
classical-register (CREG) store, classical bit instructions, and classical
control flow:
GateOp— an ordinary gate / QRAM-call opcode (same tuple layout asuniqc.circuit_builder.qcircuit.OpcodeType).MeasureOp—MEASURE q[i], c[j]: measure one qubit and write its outcome into CREG bitj(valid both mid-circuit and terminally).ResetOp—RESET q[i]: mid-circuit reset of one qubit to|0>.ClassicalOp— a classical bit instructionAND/OR/XOR/MOV/NOT(RISC three-operand, destination-first, non-destructive; operands are CREG bitsc[k]or immediates0/1).IfBlock—QIF <cond> ... [QELSE ...] ENDQIF.WhileBlock—QWHILE <cond> ... ENDQWHILE(surface syntax carries no iteration bound; the simulator enforces an internal watchdog).
Conditions (Cond) are pure boolean logic over single-bit CREG cells:
bit references c[i], the literals 0/1, the unary not/~ and
the binary and/&, xor/^, or/| (lowercase keywords and
symbols are interchangeable), with parentheses. A bare c[i] is true iff
its bit is 1. Conditions are parsed by parse_cond() and re-serialized
fully parenthesized with symbol operators for unambiguous round-tripping.
Circuit (see uniqc.circuit_builder.qcircuit) holds a structured
program in Circuit.dynamic_program (None for ordinary flat circuits).
This module owns serialization (serialize_program()), parsing
(parse_program_body()), and a structural deep-copy helper
(clone_program()).
- class uniqc.circuit_builder.classical_program.BinCond(op, left, right)[source]¶
Bases:
CondA binary boolean op over single bits:
and(&),xor(^),or(|).- left¶
- op¶
- right¶
- class uniqc.circuit_builder.classical_program.BitRef(index)[source]¶
Bases:
CondA reference to CREG bit
c[index](true iff the bit is 1).- index¶
- class uniqc.circuit_builder.classical_program.ClassicalOp(op, dest, srcs)[source]¶
Bases:
objectA classical bit instruction
AND/OR/XOR/MOV/NOTwriting CREG bit dest.- dest¶
- execute(creg)[source]¶
Read this instruction’s source operands from creg and return the resulting destination bit (0/1). Does not mutate creg.
- op¶
- srcs¶
- class uniqc.circuit_builder.classical_program.Cond[source]¶
Bases:
objectBase class for classical condition AST nodes.
- class uniqc.circuit_builder.classical_program.ConstBit(value)[source]¶
Bases:
CondA constant bit literal (0 or 1).
- value¶
- class uniqc.circuit_builder.classical_program.GateOp(opcode)[source]¶
Bases:
objectAn ordinary gate or QRAM-call opcode within a program.
- opcode¶
- class uniqc.circuit_builder.classical_program.IfBlock(cond, then_body=<factory>, else_body=None)[source]¶
Bases:
objectQIF <cond> ... [QELSE ...] ENDQIF.- cond¶
- else_body = None¶
- then_body¶
- class uniqc.circuit_builder.classical_program.MeasureOp(qubit, cbit)[source]¶
Bases:
objectMEASURE q[qubit], c[cbit]— measure qubit, write outcome to CREG cbit.- cbit¶
- qubit¶
- class uniqc.circuit_builder.classical_program.NotCond(operand)[source]¶
Bases:
CondLogical/bitwise NOT of a single-bit condition (
~x/not x).- operand¶
- class uniqc.circuit_builder.classical_program.Operand(is_imm, value)[source]¶
Bases:
objectA classical-instruction source operand: a CREG bit or a 0/1 immediate.
- is_imm¶
- value¶
- class uniqc.circuit_builder.classical_program.ResetOp(qubit)[source]¶
Bases:
objectRESET q[qubit]— reset qubit to|0>.- qubit¶
- class uniqc.circuit_builder.classical_program.WhileBlock(cond, body=<factory>, max_iterations=1000000)[source]¶
Bases:
objectQWHILE <cond> ... ENDQWHILE(internal watchdogmax_iterations).- body¶
- cond¶
- max_iterations = 1000000¶
- uniqc.circuit_builder.classical_program.clone_program(nodes)[source]¶
Recursively clone a program body list.
Returns
(new_list, list_map, node_map)wherelist_mapmapsid(old_list) -> new_listfor every body list (top-level plus every nested if/while body) andnode_mapmapsid(old_node) -> new_nodefor everyIfBlock/WhileBlock. Leaf nodes are recreated as fresh value holders.
- uniqc.circuit_builder.classical_program.contains_dynamic_keywords(originir_str)[source]¶
Return True if originir_str uses the classical / control-flow extension.
Detects control-flow and classical-instruction keywords, and also mid-circuit measurement (a
MEASUREline followed by any later gate / classical / control-flow statement — terminal-only measurement does not count).
- uniqc.circuit_builder.classical_program.imm(value)[source]¶
Construct an immediate
0/1source operand for a classical instruction (e.g.circuit.c_xor(2, 0, imm(1))forc[2] = c[0] ^ 1).This disambiguates immediates from CREG bit indices, since a bare
intpassed to theCircuit.c_*builders denotes a CREG bit indexc[int].
- uniqc.circuit_builder.classical_program.parse_cond(text)[source]¶
Parse a condition string into a
CondAST.Accepts CREG bit references
c[i], the literals0/1, the unarynot/~and the binaryand/&,xor/^,or/|(lowercase keywords and symbols interchangeable), with parentheses. Returns text unchanged if it is already aCond.
- uniqc.circuit_builder.classical_program.parse_operand(text)[source]¶
Parse a single classical-instruction operand (
c[k]or0/1).
- uniqc.circuit_builder.classical_program.parse_originir_ext_dynamic(originir_str)[source]¶
Parse dynamic OriginIR-ext text into a
Circuit.The
QRAMDECL/QINIT/CREGheader is parsed viaOriginIR_BaseParser; the body (gates,MEASURE/RESET, classical instructions, andQIF/QWHILEblocks) is parsed viaparse_program_body()and replayed through theCircuitbuilder API.- Returns:
A new
Circuitwith itsdynamic_programpopulated.
- uniqc.circuit_builder.classical_program.parse_program_body(lines, start=0)[source]¶
Parse a program body starting at
lines[start].Stops (without consuming) at a
QELSE,ENDQIF, orENDQWHILEline at this nesting level, or at end of input. Returns(body, next_index).Ordinary gate / QRAM-call lines are parsed per-line via
OriginIR_LineParser.parse_line. Block-formCONTROL/DAGGERregions are not supported inside a classical program body — use the inlinedagger/controlled_by(...)suffixes instead.