Classical Operations

MimiqCircuits allows you to perform classical logic and arithmetic operations directly on the classical register bits (c). This is essential for workflows involving feed-forward, error correction, and conditional logic.

Core Concepts

Classical operations manipulate the bits stored in the circuit’s classical register. Unlike quantum gates, these operations are irreversible (except for Not) and act on classical data.

Most logic operations in MIMIQ follow a specific pattern: 1. They take a variable number of input bits. 2. They write the result to a single destination bit.

When adding these operations to a circuit, the first index you provide is always the destination bit.

Setting and Flipping Bits

The simplest operations involve setting a bit to a specific value or flipping its current state.

SetBit0 and SetBit1

These operations force a classical bit to 0 or 1, regardless of its previous state.

from mimiqcircuits import *

c = Circuit()

# Set bit 0 to 0
c.push(SetBit0(), 0)

# Set bit 1 to 1
c.push(SetBit1(), 1)

Not (Invert)

The Not operation flips the value of a bit (0 becomes 1, 1 becomes 0).

# Flip bit 2
c.push(Not(), 2)

Boolean Logic

MIMIQ supports standard boolean logic gates. These operations can take multiple input bits and store the result in a target bit.

AND Operation

The And operation computes the logical AND of two or more inputs.

# c[0] = c[1] AND c[2]
# First index (0) is the target.
c.push(And(), 0, 1, 2)

# Multiple inputs: c[0] = c[1] AND c[2] AND c[3] AND c[4]
# We must specify the total number of bits involved (1 target + 4 inputs = 5)
c.push(And(5), 0, 1, 2, 3, 4)

OR Operation

The Or operation computes the logical OR of two or more inputs.

# c[5] = c[6] OR c[7]
c.push(Or(), 5, 6, 7)

XOR Operation

The Xor operation computes the exclusive OR (XOR) of the inputs.

# c[0] = c[1] XOR c[2]
c.push(Xor(), 0, 1, 2)

Arithmetic

Parity Check

The ParityCheck operation computes the sum of the input bits modulo 2. While functionally similar to XOR, it is often used conceptually for syndrome measurements in error correction.

# c[0] = (c[1] + c[2]) mod 2
c.push(ParityCheck(), 0, 1, 2)

# Multi-bit parity check
c.push(ParityCheck(5), 0, 1, 2, 3, 4)

Reference

class mimiqcircuits.SetBit0[source]

Bases: AbstractClassical

SetBit0 operation.

Classical operation that sets a classical bit to 0. 0 → 0 and 1 → 0.

Examples

>>> from mimiqcircuits import *
>>> not_op = SetBit0()
>>> not_op.name
'set0'
>>> c = Circuit()
>>> c.push(SetBit0(), 1)
2-bit circuit with 1 instruction:
└── c[1] = 0
inverse()[source]
iswrapper()[source]
get_operation()[source]
format_with_targets(qubits, bits, zvars)[source]
class mimiqcircuits.SetBit1[source]

Bases: AbstractClassical

SetBit1 operation.

Classical operation that sets a classical bit to 1. 0 → 1 and 1 → 1.

Examples

>>> from mimiqcircuits import *
>>> not_op = SetBit1()
>>> not_op.name
'set1'
>>> c = Circuit()
>>> c.push(SetBit1(), 1)
2-bit circuit with 1 instruction:
└── c[1] = 1
inverse()[source]
iswrapper()[source]
get_operation()[source]
format_with_targets(qubits, bits, zvars)[source]
class mimiqcircuits.Not[source]

Bases: AbstractClassical

Not operation.

Represents a NOT operation that can be added to quantum circuits. Classical operation that flips a classical bit: 0 → 1 and 1 → 0.

Examples

>>> from mimiqcircuits import *
>>> not_op = Not()
>>> not_op.name
'!'
>>> c = Circuit()
>>> c.push(Not(), 1)
2-bit circuit with 1 instruction:
└── c[1] = !c[1]
inverse()[source]
iswrapper()[source]
get_operation()[source]
format_with_targets(qubits, bits, zvars)[source]
class mimiqcircuits.And(N=3)[source]

Bases: AbstractClassical

Computes the bitwise AND of N-1 classical bits and stores the result in the first given bit.

Examples

>>> from mimiqcircuits import *
>>> And()
c[?0] = c[?1] & c[?2]
>>> And(8)
c[?0] = & @ c[?1:?7]
>>> c = Circuit()
>>> c.push(And(), 0, 2, 3)
4-bit circuit with 1 instruction:
└── c[0] = c[2] & c[3]

>>> c = Circuit()
>>> c.push(And(5), 0, 1, 2, 3, 4)
5-bit circuit with 1 instruction:
└── c[0] = c[1] & c[2] & c[3] & c[4]

>>> c = Circuit()
>>> c.push(And(8), 0, 1, 2, 3, 4, 5, 6, 7)
8-bit circuit with 1 instruction:
└── c[0] = & @ c[1, 2, 3, 4, 5, 6, 7]
__init__(N=3)[source]

Initializes an And operation.

Parameters:

N – The total number of classical bits (1 destination + N-1 inputs). Must be 3 or greater. Defaults to 3.

iswrapper()[source]
__repr__()[source]

Returns the string representation of the operation with placeholders.

__str__()[source]

Returns the simple string name of the operation.

format_with_targets(qubits, bits, zvars)[source]

Formats the operation with its specific target bits.

class mimiqcircuits.Or(N=3)[source]

Bases: AbstractClassical

Computes the bitwise OR of N-1 classical bits and stores the result in the first given bit.

Examples

>>> from mimiqcircuits import *
>>> Or()
c[?0] = c[?1] | c[?2]
>>> Or(8)
c[?0] = | @ c[?1:?7]
>>> c = Circuit()
>>> c.push(Or(), 0, 2, 3)
4-bit circuit with 1 instruction:
└── c[0] = c[2] | c[3]

>>> c = Circuit()
>>> c.push(Or(5), 0, 1, 2, 3, 4)
5-bit circuit with 1 instruction:
└── c[0] = c[1] | c[2] | c[3] | c[4]

>>> c = Circuit()
>>> c.push(Or(8), 0, 1, 2, 3, 4, 5, 6, 7)
8-bit circuit with 1 instruction:
└── c[0] = | @ c[1, 2, 3, 4, 5, 6, 7]
__init__(N=3)[source]

Initializes an Or operation.

Parameters:

N – The total number of classical bits (1 destination + N-1 inputs). Must be 3 or greater. Defaults to 3.

iswrapper()[source]
__repr__()[source]

Returns the string representation of the operation with placeholders.

__str__()[source]

Returns the simple string name of the operation.

format_with_targets(qubits, bits, zvars)[source]

Formats the operation with its specific target bits.

class mimiqcircuits.Xor(N=3)[source]

Bases: AbstractClassical

Computes the bitwise XOR of N-1 classical bits and stores the result in the first given bit.

Examples

>>> from mimiqcircuits import *
>>> Xor()
c[?0] = c[?1] ^ c[?2]
>>> Xor(8)
c[?0] = ^ @ c[?1:?7]
>>> c = Circuit()
>>> c.push(Xor(), 0, 2, 3)
4-bit circuit with 1 instruction:
└── c[0] = c[2] ^ c[3]

>>> c = Circuit()
>>> c.push(Xor(5), 0, 1, 2, 3, 4)
5-bit circuit with 1 instruction:
└── c[0] = c[1] ^ c[2] ^ c[3] ^ c[4]

>>> c = Circuit()
>>> c.push(Xor(8), 0, 1, 2, 3, 4, 5, 6, 7)
8-bit circuit with 1 instruction:
└── c[0] = ^ @ c[1, 2, 3, 4, 5, 6, 7]
__init__(N=3)[source]

Initializes an Xor operation.

Parameters:

N – The total number of classical bits (1 destination + N-1 inputs). Must be 3 or greater. Defaults to 3.

iswrapper()[source]
__repr__()[source]

Returns the string representation of the operation with placeholders.

__str__()[source]

Returns the simple string name of the operation.

format_with_targets(qubits, bits, zvars)[source]

Formats the operation with its specific target bits.

class mimiqcircuits.ParityCheck(N=3)[source]

Bases: AbstractClassical

Performs a parity check on N-1 classical bits and stores the result in the first bit.

It computes the sum modulo 2 of the inputs.

Examples

>>> from mimiqcircuits import *
>>> ParityCheck()
c[?0] = ⨊ c[?1, ?2]
>>> ParityCheck(5)
c[?0] = ⨊ c[?1, ?2, ?3, ?4]
>>> c = Circuit()
>>> c.push(ParityCheck(), 0, 2, 3)
4-bit circuit with 1 instruction:
└── c[0] = ⨊ c[2, 3]

>>> c = Circuit()
>>> c.push(ParityCheck(5), 0, 1, 2, 3, 4)
5-bit circuit with 1 instruction:
└── c[0] = ⨊ c[1, 2, 3, 4]
__init__(N=3)[source]

Initializes a ParityCheck operation.

Parameters:

N – The total number of classical bits (1 destination + N-1 inputs). Must be 3 or greater. Defaults to 3.

iswrapper()[source]
__repr__()[source]

Returns the string representation of the operation with placeholders.

__str__()[source]

Returns the simple string name of the operation.

format_with_targets(qubits, bits, zvars)[source]

Formats the operation with its specific target bits.