Compiler#
Python -> QuantumCircuit compiler.
- class quantpiler.compiler.Compiler#
Bases:
object
- add_barriers: bool = True#
- arguments: Dict[str, QReg] = {}#
- assemble(func: Callable) qiskit.circuit.QuantumCircuit #
- assemble_bit_and(srcs: List[QReg], limit: int = inf) QReg #
Calculate AND of registers.
- Parameters:
srcs (List[QReg]) – List of input registers.
limit (int, optional) – Result size limit. Defaults to float(“inf”).
- Returns:
Register with result.
- Return type:
QReg
- assemble_bit_or(srcs: List[QReg], limit: int = inf) QReg #
Calculate OR of registers.
- Parameters:
srcs (List[QReg]) – List of input registers.
limit (int, optional) – Result size limit. Defaults to float(“inf”).
- Returns:
Register with result.
- Return type:
QReg
- assemble_copy(src: QReg, trg: None | QReg = None) QReg #
- assemble_function(func: AST)#
- assemble_if(inst: If)#
Assemble ast.If operation.
- Parameters:
inst (ast.If) – Operation to assemble.
- assemble_instruction(instruction: AST)#
- assemble_instructions(instructions: List[AST])#
- assemble_invert(src: QReg, limit: int = inf) QReg #
Invert register.
This will flip all the qubits in the register.
- Parameters:
src (QReg) – Register to invert.
limit (int, optional) – Result size limit. Defaults to float(“inf”).
- Returns:
Inverted register.
- Return type:
QReg
- assemble_lshift(src: QReg, distance: int, limit: int = inf) QReg #
Calculate LShift of register.
Add distance bits at the beginng and drop bits over limit at the end.
- Parameters:
src (QReg) – Input register.
distance (int) – Shift distance.
limit (int, optional) – Result size limit. Defaults to float(“inf”).
- Returns:
Register with result.
- Return type:
QReg
- assemble_op(op: AST, limit: int = inf) QReg #
- assemble_rshift(src: QReg, distance: int, limit: int = inf) QReg #
Calculate RShift of register.
- Parameters:
src (QReg) – Input register.
distance (int) – Shift distance.
limit (int, optional) – Result size limit. Defaults to float(“inf”).
- Returns:
Register with result.
- Return type:
QReg
- assemble_to_bool(src: QReg, prev: qiskit.circuit.quantumregister.Qubit | None = None) qiskit.circuit.quantumregister.Qubit #
- assemble_xor(srcs: List[QReg], limit: int = inf) QReg #
Calculate XOR of registers.
- Parameters:
srcs (List[QReg]) – List of input registers.
limit (int, optional) – Result size limit. Defaults to float(“inf”).
- Returns:
Register with result.
- Return type:
QReg
- barrier()#
Adds a barrier to the circuit if they are enabled.
- bits: List[qiskit.circuit.quantumregister.Qubit] = []#
- conditions: List[qiskit.circuit.quantumregister.Qubit] = []#
- create_bit() qiskit.circuit.quantumregister.Qubit #
Create qubit and add it to QuantumCircuit.
- Returns:
The created qubit.
- Return type:
Qubit
- create_reg(size: int) QReg #
Create register of given size.
- Parameters:
size (int) – Number of qubits in the register.
- Returns:
Created register.
- Return type:
QReg
- cx(src, trg)#
- destroy_reg(reg: QReg)#
- drop_bit(bit: qiskit.circuit.quantumregister.Qubit)#
Add a qubit to the stack of unused qubits.
- Parameters:
bit (Qubit) – Unused qubit.
- drop_tmp_reg(reg: QReg)#
Drop a register if it is temporary.
- Parameters:
reg (QReg) – Register to check and drop.
- drop_tmp_regs(regs: List[QReg])#
- drop_unused_bits(used_reg: QReg, unused_reg: QReg)#
Drop qubits that are in unused_reg but not in used_reg.
- Parameters:
used_reg (QReg) – Register in use.
unused_reg (QReg) – Unused register.
- get_bit() qiskit.circuit.quantumregister.Qubit #
Return qubit in 0 state.
If an unused qubit exists, it will be returned, otherwise a new one will be created.
- Returns:
Qubit in 0 state.
- Return type:
Qubit
- get_qc() qiskit.circuit.QuantumCircuit #
- get_ret() qiskit.QuantumRegister #
- mcx(srcs, trg)#
- op_to_reg(op: AST) QReg #
Perform an operation and return the resulting register.
- Parameters:
op (ast.AST) – AST operation to execute.
- Returns:
Resulting register.
- Return type:
QReg
- ops_to_regs(ops: List[AST]) List[QReg] #
Perform all operations and return a list of registers.
- Parameters:
ops (List[ast.AST]) – List of AST operations to execute.
- Returns:
List of resulting registers.
- Return type:
List[QReg]
- qc: qiskit.circuit.QuantumCircuit = None#
- reg_from_const(data: int) QReg #
Create a register with the specified number.
- Parameters:
data (int) – Register value.
- Returns:
Register with the specified number.
- Return type:
QReg
- resize_reg(reg: QReg, size: int) QReg #
Resize register.
If the new size is larger than the current size, new qubits will be added to the end of the register. If not, the extra qubits at the end of register will be removed.
- Parameters:
reg (QReg) – Register to resize.
size (int) – The new register size.
- Raises:
ValueError – Invalid arguments.
- Returns:
The resized register.
- Return type:
QReg
- ret: QReg = None#
- set_qc(qc: qiskit.circuit.QuantumCircuit)#
- swap(trg1, trg2)#
- variables: Dict[str, QReg] = {}#
- x(trg)#
- quantpiler.compiler.get_args_vars(func: Callable) Dict[str, int] #
- quantpiler.compiler.get_ast(module) AST #
Get the AST of the python module.
- Parameters:
module – Python module.
- Returns:
The resulting AST.
- Return type:
ast.AST
- quantpiler.compiler.get_max_reg(regs: List[QReg]) QReg | None #
Get the register with the maximum size.
- Parameters:
regs (List[QReg]) – Registers.
- Returns:
The largest register if the regs is not empty and None otherwise.
- Return type:
Union[QReg, None]
- quantpiler.compiler.get_max_tmp_src(srcs: List[QReg]) QReg | None #
- quantpiler.compiler.get_min_reg(regs: List[QReg]) QReg | None #
Get the register with the minimum size.
- Parameters:
regs (List[QReg]) – Registers.
- Returns:
The smallest register if the regs is not empty and None otherwise.
- Return type:
Union[QReg, None]
- quantpiler.compiler.get_min_tmp_src(srcs: List[QReg]) QReg #
- quantpiler.compiler.get_return_size(func: Callable) int #
Get the return size of the quantum function.
- Parameters:
func (Callable) – Quantum function.
- Returns:
0 if the function returns nothing, otherwise the return size.
- Return type:
int
- quantpiler.compiler.get_tmp_regs(regs: List[QReg]) List[QReg] #
Get all temporary registers from the given registers.
- Parameters:
regs (List[QReg]) – A list of registers in which to find temporary registers.
- Returns:
Temporary registers.
- Return type:
List[QReg]
- quantpiler.compiler.get_used_vars(op: AST) List[str] #
- quantpiler.compiler.unwrap_ops_chain(op: AST, t: AST) List[AST] #