Quantum circuit representation and execution
Quantum circuit representation and execution
(add-gate circuit
gate-type
&
{:keys [target target1 target2 control control1 control2 angle]
:as params})
Add a quantum gate to a quantum circuit.
This function builds quantum circuits by appending unitary gates. Gates are represented as operations with specific gate types and parameters.
Parameters:
Returns: Updated quantum circuit with the new gate appended to the :operations vector
Examples: (add-gate (create-circuit 2) :x :target 0) ;=> {:operations [{:operation-type :x, :operation-params {:target 0}}], :num-qubits 2}
(add-gate (create-circuit 2) :cnot :control 0 :target 1) ;=> {:operations [{:operation-type :cnot, :operation-params {:control 0, :target 1}}], :num-qubits 2}
(add-gate (create-circuit 1) :rx :target 0 :angle 1.57) ;=> {:operations [{:operation-type :rx, :operation-params {:target 0, :angle 1.57}}], :num-qubits 1}
Add a quantum gate to a quantum circuit. This function builds quantum circuits by appending unitary gates. Gates are represented as operations with specific gate types and parameters. Parameters: - circuit: A quantum circuit map (created with create-circuit) - gate-type: Keyword identifying the type of gate (:x, :y, :z, :h, :cnot, etc.) - target: (optional) Integer index of the target qubit (0-indexed) - target1: (optional) Integer index of the target1 qubit (0-indexed) - target2: (optional) Integer index of the target2 qubit (0-indexed) - control: (optional) Integer index of the control qubit for controlled gates - control1: (optional) Integer index of the control1 qubit for controlled gates - control2: (optional) Integer index of the control2 qubit for controlled gates - angle: (optional) Rotation angle for parameterized gates (in radians) Returns: Updated quantum circuit with the new gate appended to the :operations vector Examples: (add-gate (create-circuit 2) :x :target 0) ;=> {:operations [{:operation-type :x, :operation-params {:target 0}}], :num-qubits 2} (add-gate (create-circuit 2) :cnot :control 0 :target 1) ;=> {:operations [{:operation-type :cnot, :operation-params {:control 0, :target 1}}], :num-qubits 2} (add-gate (create-circuit 1) :rx :target 0 :angle 1.57) ;=> {:operations [{:operation-type :rx, :operation-params {:target 0, :angle 1.57}}], :num-qubits 1}
(add-operation circuit operation-type params)
Add a quantum operation (gate or measurement) to a quantum circuit.
This is the core function for building quantum circuits by appending operations to the operation sequence. Operations can be either unitary gates or non-unitary measurements.
Parameters:
Returns: Updated quantum circuit with the new operation appended to the :operations vector
Add a quantum operation (gate or measurement) to a quantum circuit. This is the core function for building quantum circuits by appending operations to the operation sequence. Operations can be either unitary gates or non-unitary measurements. Parameters: - circuit: A quantum circuit map (created with create-circuit) - operation-type: Keyword identifying the type of operation (:x, :y, :z, :h, :cnot, :measure, etc.) - params: Map of operation parameters Returns: Updated quantum circuit with the new operation appended to the :operations vector
(all-gates-circuit)
Create a quantum circuit that demonstrates all supported gates.
Parameters: None
Returns: Quantum circuit that contains all supported gates
Create a quantum circuit that demonstrates all supported gates. Parameters: None Returns: Quantum circuit that contains all supported gates
(apply-gate-to-state state gate)
Apply a single quantum gate to a quantum state.
This is an internal function that translates circuit gate representations into actual quantum gate operations on quantum states. It handles the mapping from gate types to the corresponding gate functions.
Note: This function only handles unitary gates, not measurements.
Parameters:
Returns: New quantum state after applying the gate
Example: (apply-gate-to-state |0⟩ {:operation-type :x, :operation-params {:target 0}}) ;=> |1⟩ state
Apply a single quantum gate to a quantum state. This is an internal function that translates circuit gate representations into actual quantum gate operations on quantum states. It handles the mapping from gate types to the corresponding gate functions. Note: This function only handles unitary gates, not measurements. Parameters: - state: Quantum state to apply the gate to - gate: Gate map containing :operation-type and optional :operation-params Returns: New quantum state after applying the gate Example: (apply-gate-to-state |0⟩ {:operation-type :x, :operation-params {:target 0}}) ;=> |1⟩ state
(apply-measurement-to-state state measurement)
Apply a measurement operation to a quantum state.
This function handles non-unitary measurement operations that collapse the quantum state.
Parameters:
Returns: New quantum state after measurement collapse
Apply a measurement operation to a quantum state. This function handles non-unitary measurement operations that collapse the quantum state. Parameters: - state: Quantum state to measure - measurement: Measurement operation map Returns: New quantum state after measurement collapse
(apply-operation-to-state state operation)
Apply a quantum operation (gate or measurement) to a quantum state.
This function dispatches between unitary gates and non-unitary operations like measurements.
Parameters:
Returns: New quantum state after applying the operation
Apply a quantum operation (gate or measurement) to a quantum state. This function dispatches between unitary gates and non-unitary operations like measurements. Parameters: - state: Quantum state to apply the operation to - operation: Operation map containing :operation-type and :operation-params Returns: New quantum state after applying the operation
(bell-state-circuit)
Create a Bell state preparation circuit.
Creates a quantum circuit that prepares the Bell state (|00⟩ + |11⟩)/√2 from the initial state |00⟩. This is achieved by applying a Hadamard gate to the first qubit followed by a CNOT gate.
The Bell state is a maximally entangled two-qubit state fundamental to quantum information protocols like quantum teleportation and superdense coding.
Parameters: None
Returns: Quantum circuit that prepares Bell state when applied to |00⟩
Example: (def bell-circuit (bell-state-circuit)) (execute-circuit bell-circuit (qs/zero-state 2)) ;=> Bell state (|00⟩ + |11⟩)/√2
Create a Bell state preparation circuit. Creates a quantum circuit that prepares the Bell state (|00⟩ + |11⟩)/√2 from the initial state |00⟩. This is achieved by applying a Hadamard gate to the first qubit followed by a CNOT gate. The Bell state is a maximally entangled two-qubit state fundamental to quantum information protocols like quantum teleportation and superdense coding. Parameters: None Returns: Quantum circuit that prepares Bell state when applied to |00⟩ Example: (def bell-circuit (bell-state-circuit)) (execute-circuit bell-circuit (qs/zero-state 2)) ;=> Bell state (|00⟩ + |11⟩)/√2
(circuit-depth circuit)
Calculate the depth (number of sequential operation layers) of a quantum circuit.
Circuit depth is the minimum number of time steps needed to execute the circuit, assuming operations that operate on different qubits can be executed in parallel. This is an important metric for circuit optimization and noise analysis.
Note: Measurements are treated as operations that can be parallelized with gates on different qubits but require their own time step.
Parameters:
Returns: Integer representing the circuit depth
Example: (circuit-depth bell-circuit) ;=> 2 (H gate in layer 1, CNOT in layer 2)
Calculate the depth (number of sequential operation layers) of a quantum circuit. Circuit depth is the minimum number of time steps needed to execute the circuit, assuming operations that operate on different qubits can be executed in parallel. This is an important metric for circuit optimization and noise analysis. Note: Measurements are treated as operations that can be parallelized with gates on different qubits but require their own time step. Parameters: - circuit: Quantum circuit to analyze Returns: Integer representing the circuit depth Example: (circuit-depth bell-circuit) ;=> 2 (H gate in layer 1, CNOT in layer 2)
(circuit-gate-count circuit)
Count the number of gates (excluding measurements) in a quantum circuit.
Provides a metric for unitary operation complexity.
Parameters:
Returns: Integer representing the number of gates (excluding measurements)
Example: (circuit-gate-count bell-circuit) ;=> 2 (H gate + CNOT gate)
Count the number of gates (excluding measurements) in a quantum circuit. Provides a metric for unitary operation complexity. Parameters: - circuit: Quantum circuit to analyze Returns: Integer representing the number of gates (excluding measurements) Example: (circuit-gate-count bell-circuit) ;=> 2 (H gate + CNOT gate)
(circuit-gate-types circuit)
Get a summary of gate types used in the circuit (excluding measurements).
Returns a map with gate types as keys and counts as values, useful for analyzing circuit composition.
Parameters:
Returns: Map of gate type keywords to counts
Example: (circuit-gate-types bell-circuit) ;=> {:h 1, :cnot 1}
Get a summary of gate types used in the circuit (excluding measurements). Returns a map with gate types as keys and counts as values, useful for analyzing circuit composition. Parameters: - circuit: Quantum circuit to analyze Returns: Map of gate type keywords to counts Example: (circuit-gate-types bell-circuit) ;=> {:h 1, :cnot 1}
(circuit-operation-count circuit)
Count the total number of operations in a quantum circuit.
Provides a simple metric for circuit complexity and resource requirements. Includes both gates and measurements.
Parameters:
Returns: Integer representing the total number of operations
Example: (circuit-operation-count bell-circuit) ;=> 2 (H gate + CNOT gate)
Count the total number of operations in a quantum circuit. Provides a simple metric for circuit complexity and resource requirements. Includes both gates and measurements. Parameters: - circuit: Quantum circuit to analyze Returns: Integer representing the total number of operations Example: (circuit-operation-count bell-circuit) ;=> 2 (H gate + CNOT gate)
(circuit-operation-types circuit)
Get a summary of operation types used in the circuit.
Returns a map with operation types as keys and counts as values, useful for analyzing circuit composition. Includes both gates and measurements.
Parameters:
Returns: Map of operation type keywords to counts
Example: (circuit-operation-types bell-circuit-with-measurement) ;=> {:h 1, :cnot 1, :measure 1}
Get a summary of operation types used in the circuit. Returns a map with operation types as keys and counts as values, useful for analyzing circuit composition. Includes both gates and measurements. Parameters: - circuit: Quantum circuit to analyze Returns: Map of operation type keywords to counts Example: (circuit-operation-types bell-circuit-with-measurement) ;=> {:h 1, :cnot 1, :measure 1}
(cnot-gate circuit control target)
Add a CNOT (Controlled-NOT) gate to the quantum circuit.
The CNOT gate flips the target qubit if and only if the control qubit is |1⟩. It's a fundamental two-qubit gate used for creating entanglement.
Truth table:
|00⟩ → |00⟩
|01⟩ → |01⟩
|10⟩ → |11⟩
|11⟩ → |10⟩
Parameters:
Returns: Updated quantum circuit with CNOT gate appended
Example: (cnot-gate (create-circuit 2) 0 1) ;=> Circuit with CNOT gate, control on qubit 0, target on qubit 1
Add a CNOT (Controlled-NOT) gate to the quantum circuit. The CNOT gate flips the target qubit if and only if the control qubit is |1⟩. It's a fundamental two-qubit gate used for creating entanglement. Truth table: |00⟩ → |00⟩ |01⟩ → |01⟩ |10⟩ → |11⟩ |11⟩ → |10⟩ Parameters: - circuit: Quantum circuit to add the gate to - control: Integer index of the control qubit (0-indexed) - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with CNOT gate appended Example: (cnot-gate (create-circuit 2) 0 1) ;=> Circuit with CNOT gate, control on qubit 0, target on qubit 1
(controlled-gate circuit gate-type control target)
Add a general controlled gate to the quantum circuit.
Creates a controlled version of any single-qubit gate, where the gate is applied to the target qubit only if the control qubit is |1⟩.
Parameters:
Returns: Updated quantum circuit with controlled gate appended
Example: (controlled-gate (create-circuit 2) :z 0 1) ;=> Circuit with controlled-Z gate, control on qubit 0, target on qubit 1
Add a general controlled gate to the quantum circuit. Creates a controlled version of any single-qubit gate, where the gate is applied to the target qubit only if the control qubit is |1⟩. Parameters: - circuit: Quantum circuit to add the gate to - gate-type: Keyword specifying the type of gate to control (:x, :y, :z, :h, etc.) - control: Integer index of the control qubit (0-indexed) - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with controlled gate appended Example: (controlled-gate (create-circuit 2) :z 0 1) ;=> Circuit with controlled-Z gate, control on qubit 0, target on qubit 1
(create-circuit num-qubits)
(create-circuit num-qubits name)
(create-circuit num-qubits name description)
Create a new quantum circuit.
A quantum circuit is a data structure representing a sequence of quantum operations to be applied to a quantum system. This function initializes an empty circuit with the specified number of qubits.
Parameters:
Returns: A quantum circuit map containing:
Examples: (create-circuit 2) ;=> {:operations [], :num-qubits 2}
(create-circuit 3 "My Circuit") ;=> {:operations [], :num-qubits 3, :name "My Circuit"}
(create-circuit 2 "Bell State" "Prepares entangled Bell state") ;=> {:operations [], :num-qubits 2, :name "Bell State", :description "Prepares entangled Bell state"}
Create a new quantum circuit. A quantum circuit is a data structure representing a sequence of quantum operations to be applied to a quantum system. This function initializes an empty circuit with the specified number of qubits. Parameters: - num-qubits: Positive integer specifying the number of qubits in the circuit - name: (optional) String name for the circuit for identification - description: (optional) String description explaining what the circuit does Returns: A quantum circuit map containing: - :operations - Empty vector to hold quantum operations (gates + measurements) - :num-qubits - Number of qubits the circuit operates on - :name - Optional name for the circuit - :description - Optional description of the circuit's purpose Examples: (create-circuit 2) ;=> {:operations [], :num-qubits 2} (create-circuit 3 "My Circuit") ;=> {:operations [], :num-qubits 3, :name "My Circuit"} (create-circuit 2 "Bell State" "Prepares entangled Bell state") ;=> {:operations [], :num-qubits 2, :name "Bell State", :description "Prepares entangled Bell state"}
(crx-gate circuit control target angle)
Add a controlled RX gate to the quantum circuit.
The CRX gate applies an RX rotation to the target qubit only when the control qubit is |1⟩.
Parameters:
Returns: Updated quantum circuit with CRX gate appended
Example: (crx-gate (create-circuit 2) 0 1 (/ Math/PI 4)) ;=> Circuit with CRX(π/4) gate, control on qubit 0, target on qubit 1
Add a controlled RX gate to the quantum circuit. The CRX gate applies an RX rotation to the target qubit only when the control qubit is |1⟩. Parameters: - circuit: Quantum circuit to add the gate to - control: Integer index of the control qubit (0-indexed) - target: Integer index of the target qubit (0-indexed) - angle: Rotation angle in radians Returns: Updated quantum circuit with CRX gate appended Example: (crx-gate (create-circuit 2) 0 1 (/ Math/PI 4)) ;=> Circuit with CRX(π/4) gate, control on qubit 0, target on qubit 1
(cry-gate circuit control target angle)
Add a controlled RY gate to the quantum circuit.
The CRY gate applies an RY rotation to the target qubit only when the control qubit is |1⟩.
Parameters:
Returns: Updated quantum circuit with CRY gate appended
Example: (cry-gate (create-circuit 2) 0 1 (/ Math/PI 3)) ;=> Circuit with CRY(π/3) gate, control on qubit 0, target on qubit 1
Add a controlled RY gate to the quantum circuit. The CRY gate applies an RY rotation to the target qubit only when the control qubit is |1⟩. Parameters: - circuit: Quantum circuit to add the gate to - control: Integer index of the control qubit (0-indexed) - target: Integer index of the target qubit (0-indexed) - angle: Rotation angle in radians Returns: Updated quantum circuit with CRY gate appended Example: (cry-gate (create-circuit 2) 0 1 (/ Math/PI 3)) ;=> Circuit with CRY(π/3) gate, control on qubit 0, target on qubit 1
(crz-gate circuit control target angle)
Add a controlled RZ gate to the quantum circuit.
The CRZ gate applies an RZ rotation to the target qubit only when the control qubit is |1⟩. Essential for quantum algorithms like QFT.
Parameters:
Returns: Updated quantum circuit with CRZ gate appended
Example: (crz-gate (create-circuit 2) 0 1 (/ Math/PI 2)) ;=> Circuit with CRZ(π/2) gate, control on qubit 0, target on qubit 1
Add a controlled RZ gate to the quantum circuit. The CRZ gate applies an RZ rotation to the target qubit only when the control qubit is |1⟩. Essential for quantum algorithms like QFT. Parameters: - circuit: Quantum circuit to add the gate to - control: Integer index of the control qubit (0-indexed) - target: Integer index of the target qubit (0-indexed) - angle: Rotation angle in radians Returns: Updated quantum circuit with CRZ gate appended Example: (crz-gate (create-circuit 2) 0 1 (/ Math/PI 2)) ;=> Circuit with CRZ(π/2) gate, control on qubit 0, target on qubit 1
(cy-gate circuit control target)
Add a Controlled-Y gate to the quantum circuit.
The CY gate applies a Y gate (bit and phase flip) to the target qubit only when the control qubit is in state |1⟩.
Truth table: |00⟩ → |00⟩ |01⟩ → |01⟩ |10⟩ → i|11⟩ (target flipped with i phase) |11⟩ → -i|10⟩ (target flipped with -i phase)
Parameters:
Returns: Updated quantum circuit with CY gate appended
Example: (cy-gate (create-circuit 2) 0 1) ;=> Circuit with CY gate, control on qubit 0, target on qubit 1
Add a Controlled-Y gate to the quantum circuit. The CY gate applies a Y gate (bit and phase flip) to the target qubit only when the control qubit is in state |1⟩. Truth table: |00⟩ → |00⟩ |01⟩ → |01⟩ |10⟩ → i|11⟩ (target flipped with i phase) |11⟩ → -i|10⟩ (target flipped with -i phase) Parameters: - circuit: Quantum circuit to add the gate to - control: Integer index of the control qubit (0-indexed) - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with CY gate appended Example: (cy-gate (create-circuit 2) 0 1) ;=> Circuit with CY gate, control on qubit 0, target on qubit 1
(cz-gate circuit control target)
Add a Controlled-Z gate to the quantum circuit.
The CZ gate applies a phase flip (Z gate) to the target qubit only when the control qubit is in state |1⟩. It's symmetric - either qubit can be considered the control or target.
Truth table:
|00⟩ → |00⟩
|01⟩ → |01⟩
|10⟩ → |10⟩
|11⟩ → -|11⟩ (negative phase applied only to |11⟩ state)
Parameters:
Returns: Updated quantum circuit with CZ gate appended
Example: (cz-gate (create-circuit 2) 0 1) ;=> Circuit with CZ gate, control on qubit 0, target on qubit 1
Add a Controlled-Z gate to the quantum circuit. The CZ gate applies a phase flip (Z gate) to the target qubit only when the control qubit is in state |1⟩. It's symmetric - either qubit can be considered the control or target. Truth table: |00⟩ → |00⟩ |01⟩ → |01⟩ |10⟩ → |10⟩ |11⟩ → -|11⟩ (negative phase applied only to |11⟩ state) Parameters: - circuit: Quantum circuit to add the gate to - control: Integer index of the control qubit (0-indexed) - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with CZ gate appended Example: (cz-gate (create-circuit 2) 0 1) ;=> Circuit with CZ gate, control on qubit 0, target on qubit 1
(execute-circuit circuit initial-state)
Execute a quantum circuit on an initial quantum state.
Applies all operations in the circuit sequentially to transform the initial quantum state into the final state. This is the main function for running quantum computations.
Parameters:
Returns: Final quantum state after applying all operations in the circuit
Throws: Exception if circuit and state have mismatched number of qubits
Example: (execute-circuit (bell-state-circuit) (qs/zero-state 2)) ;=> Bell state (|00⟩ + |11⟩)/√2
Execute a quantum circuit on an initial quantum state. Applies all operations in the circuit sequentially to transform the initial quantum state into the final state. This is the main function for running quantum computations. Parameters: - circuit: Quantum circuit containing the sequence of operations to apply - initial-state: Initial quantum state to start the computation from Returns: Final quantum state after applying all operations in the circuit Throws: Exception if circuit and state have mismatched number of qubits Example: (execute-circuit (bell-state-circuit) (qs/zero-state 2)) ;=> Bell state (|00⟩ + |11⟩)/√2
(fredkin-gate circuit control target1 target2)
Add a Fredkin (CSWAP) gate to the quantum circuit.
The Fredkin gate is a three-qubit gate that swaps the states of the two target qubits if and only if the control qubit is in state |1⟩. It is also known as the controlled-SWAP gate.
Parameters:
Returns: Updated quantum circuit with Fredkin gate appended
Example: (fredkin-gate (create-circuit 3) 0 1 2) ;=> Circuit with Fredkin gate with control on qubit 0, swapping qubits 1 and 2
Add a Fredkin (CSWAP) gate to the quantum circuit. The Fredkin gate is a three-qubit gate that swaps the states of the two target qubits if and only if the control qubit is in state |1⟩. It is also known as the controlled-SWAP gate. Parameters: - circuit: Quantum circuit to add the gate to - control: Integer index of the control qubit (0-indexed) - target1: Integer index of the first target qubit to swap (0-indexed) - target2: Integer index of the second target qubit to swap (0-indexed) Returns: Updated quantum circuit with Fredkin gate appended Example: (fredkin-gate (create-circuit 3) 0 1 2) ;=> Circuit with Fredkin gate with control on qubit 0, swapping qubits 1 and 2
(ghz-state-circuit n)
Create a GHZ (Greenberger-Horne-Zeilinger) state preparation circuit.
Creates a quantum circuit that prepares an n-qubit GHZ state of the form (|00...0⟩ + |11...1⟩)/√2 from the initial state |00...0⟩.
The GHZ state is a maximally entangled n-qubit state that exhibits non-classical correlations and is used in quantum cryptography and tests of quantum mechanics.
Parameters:
Returns: Quantum circuit that prepares n-qubit GHZ state when applied to |00...0⟩
Example: (def ghz3-circuit (ghz-state-circuit 3)) (execute-circuit ghz3-circuit (qs/zero-state 3)) ;=> 3-qubit GHZ state (|000⟩ + |111⟩)/√2
Create a GHZ (Greenberger-Horne-Zeilinger) state preparation circuit. Creates a quantum circuit that prepares an n-qubit GHZ state of the form (|00...0⟩ + |11...1⟩)/√2 from the initial state |00...0⟩. The GHZ state is a maximally entangled n-qubit state that exhibits non-classical correlations and is used in quantum cryptography and tests of quantum mechanics. Parameters: - n: Number of qubits (must be ≥ 2) Returns: Quantum circuit that prepares n-qubit GHZ state when applied to |00...0⟩ Example: (def ghz3-circuit (ghz-state-circuit 3)) (execute-circuit ghz3-circuit (qs/zero-state 3)) ;=> 3-qubit GHZ state (|000⟩ + |111⟩)/√2
(h-gate circuit target)
Add a Hadamard gate to the quantum circuit.
The Hadamard gate creates superposition: |0⟩ → (|0⟩ + |1⟩)/√2 and |1⟩ → (|0⟩ - |1⟩)/√2. It's fundamental for creating quantum superposition states.
Parameters:
Returns: Updated quantum circuit with Hadamard gate appended
Example: (h-gate (create-circuit 2) 0) ;=> Circuit with Hadamard gate on qubit 0
Add a Hadamard gate to the quantum circuit. The Hadamard gate creates superposition: |0⟩ → (|0⟩ + |1⟩)/√2 and |1⟩ → (|0⟩ - |1⟩)/√2. It's fundamental for creating quantum superposition states. Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with Hadamard gate appended Example: (h-gate (create-circuit 2) 0) ;=> Circuit with Hadamard gate on qubit 0
(inverse-circuit circuit)
Create the inverse (adjoint) of a quantum circuit.
The inverse circuit undoes the effect of the original circuit. It applies the inverse of each unitary operation in reverse order. Measurement operations are filtered out as they cannot be inverted.
Parameters:
Returns: New quantum circuit that is the inverse of the input circuit
Example: (inverse-circuit (bell-state-circuit)) ;=> Circuit that converts Bell state back to |00⟩
Create the inverse (adjoint) of a quantum circuit. The inverse circuit undoes the effect of the original circuit. It applies the inverse of each unitary operation in reverse order. Measurement operations are filtered out as they cannot be inverted. Parameters: - circuit: Quantum circuit to invert Returns: New quantum circuit that is the inverse of the input circuit Example: (inverse-circuit (bell-state-circuit)) ;=> Circuit that converts Bell state back to |00⟩
(inverse-gate gate)
Get the inverse (adjoint) of a quantum gate.
Returns the quantum gate that undoes the effect of the input gate. For unitary gates, this is the complex conjugate transpose.
Parameters:
Returns: Gate map representing the inverse gate
Example: (inverse-gate {:operation-type :s, :operation-params {:target 0}}) ;=> {:operation-type :s-dagger, :operation-params {:target 0}}
Get the inverse (adjoint) of a quantum gate. Returns the quantum gate that undoes the effect of the input gate. For unitary gates, this is the complex conjugate transpose. Parameters: - gate: Gate map containing :gate-type and optional :gate-params Returns: Gate map representing the inverse gate Example: (inverse-gate {:operation-type :s, :operation-params {:target 0}}) ;=> {:operation-type :s-dagger, :operation-params {:target 0}}
(inverse-operation operation)
Get the inverse (adjoint) of a quantum operation.
For unitary gates, returns the inverse gate operation. For measurements, returns nil as measurements cannot be inverted.
Parameters:
Returns: Operation map representing the inverse operation, or nil for measurements
Get the inverse (adjoint) of a quantum operation. For unitary gates, returns the inverse gate operation. For measurements, returns nil as measurements cannot be inverted. Parameters: - operation: Operation map containing :operation-type and optional :operation-params Returns: Operation map representing the inverse operation, or nil for measurements
(iswap-gate circuit qubit1 qubit2)
Add an iSWAP gate to the quantum circuit.
The iSWAP gate exchanges the states of two qubits with an additional phase factor i. It transforms |01⟩ → i|10⟩ and |10⟩ → i|01⟩, while leaving |00⟩ and |11⟩ unchanged.
Parameters:
Returns: Updated quantum circuit with iSWAP gate appended
Example: (iswap-gate (create-circuit 2) 0 1) ;=> Circuit with iSWAP gate between qubits 0 and 1
Add an iSWAP gate to the quantum circuit. The iSWAP gate exchanges the states of two qubits with an additional phase factor i. It transforms |01⟩ → i|10⟩ and |10⟩ → i|01⟩, while leaving |00⟩ and |11⟩ unchanged. Parameters: - circuit: Quantum circuit to add the gate to - qubit1: Integer index of the first qubit (0-indexed) - qubit2: Integer index of the second qubit (0-indexed) Returns: Updated quantum circuit with iSWAP gate appended Example: (iswap-gate (create-circuit 2) 0 1) ;=> Circuit with iSWAP gate between qubits 0 and 1
(measure-all-operation circuit)
Add a measurement operation for all qubits in the circuit.
Convenience function that measures all qubits in the quantum circuit. This is equivalent to calling (measure-operation circuit (range num-qubits)).
Parameters:
Returns: Updated quantum circuit with measurement of all qubits
Example: (measure-all-operation (create-circuit 3)) ;=> Circuit with measurement of qubits [0 1 2]
Add a measurement operation for all qubits in the circuit. Convenience function that measures all qubits in the quantum circuit. This is equivalent to calling (measure-operation circuit (range num-qubits)). Parameters: - circuit: Quantum circuit to add the measurement to Returns: Updated quantum circuit with measurement of all qubits Example: (measure-all-operation (create-circuit 3)) ;=> Circuit with measurement of qubits [0 1 2]
(measure-operation circuit qubits)
Add a measurement operation to the quantum circuit.
The measurement operation performs a quantum measurement in the computational basis on the specified qubits. This collapses the quantum state and produces classical measurement outcomes. Unlike gates, measurements are non-unitary operations.
Parameters:
Returns: Updated quantum circuit with measurement operation appended
Example: (measure-operation (create-circuit 2) [0 1]) ;=> Circuit with measurement of qubits 0 and 1
Add a measurement operation to the quantum circuit. The measurement operation performs a quantum measurement in the computational basis on the specified qubits. This collapses the quantum state and produces classical measurement outcomes. Unlike gates, measurements are non-unitary operations. Parameters: - circuit: Quantum circuit to add the measurement to - qubits: Vector of qubit indices to measure (0-indexed) Returns: Updated quantum circuit with measurement operation appended Example: (measure-operation (create-circuit 2) [0 1]) ;=> Circuit with measurement of qubits 0 and 1
(measure-subsystem state qubit-indices)
Measure specific qubits and return just the measurement outcome.
This is a convenience function that combines partial trace with measurement for algorithms that only care about specific qubit outcomes.
Measure specific qubits and return just the measurement outcome. This is a convenience function that combines partial trace with measurement for algorithms that only care about specific qubit outcomes.
(phase-gate circuit target angle)
Add a phase gate with arbitrary phase to the quantum circuit.
The phase gate applies a phase rotation only to the |1⟩ component: P(φ)|0⟩ = |0⟩ P(φ)|1⟩ = e^(iφ)|1⟩
Parameters:
Returns: Updated quantum circuit with phase gate appended
Example: (phase-gate (create-circuit 1) 0 (/ Math/PI 2)) ; Same as S gate ;=> Circuit with P(π/2) gate on qubit 0
Add a phase gate with arbitrary phase to the quantum circuit. The phase gate applies a phase rotation only to the |1⟩ component: P(φ)|0⟩ = |0⟩ P(φ)|1⟩ = e^(iφ)|1⟩ Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) - angle: Phase angle in radians Returns: Updated quantum circuit with phase gate appended Example: (phase-gate (create-circuit 1) 0 (/ Math/PI 2)) ; Same as S gate ;=> Circuit with P(π/2) gate on qubit 0
(print-circuit circuit)
Print a human-readable representation of a quantum circuit.
Displays the circuit gates in a formatted way showing gate types, target qubits, control qubits, and parameters for easy visualization and debugging.
Parameters:
Returns: nil (prints to stdout)
Example: (print-circuit bell-circuit) ;; Bell State (2 qubits): ;; H(0) ;; CNOT(0→1)
Print a human-readable representation of a quantum circuit. Displays the circuit gates in a formatted way showing gate types, target qubits, control qubits, and parameters for easy visualization and debugging. Parameters: - circuit: Quantum circuit to print Returns: nil (prints to stdout) Example: (print-circuit bell-circuit) ;; Bell State (2 qubits): ;; H(0) ;; CNOT(0→1)
(rx-gate circuit target angle)
Add a rotation around the X-axis gate to the quantum circuit.
The RX gate rotates a qubit around the X-axis of the Bloch sphere by the specified angle. RX(θ) = cos(θ/2)I - i*sin(θ/2)X
Parameters:
Returns: Updated quantum circuit with RX gate appended
Example: (rx-gate (create-circuit 1) 0 1.57) ; π/2 rotation ;=> Circuit with RX(π/2) gate on qubit 0
Add a rotation around the X-axis gate to the quantum circuit. The RX gate rotates a qubit around the X-axis of the Bloch sphere by the specified angle. RX(θ) = cos(θ/2)I - i*sin(θ/2)X Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) - angle: Rotation angle in radians Returns: Updated quantum circuit with RX gate appended Example: (rx-gate (create-circuit 1) 0 1.57) ; π/2 rotation ;=> Circuit with RX(π/2) gate on qubit 0
(ry-gate circuit target angle)
Add a rotation around the Y-axis gate to the quantum circuit.
The RY gate rotates a qubit around the Y-axis of the Bloch sphere by the specified angle. RY(θ) = cos(θ/2)I - i*sin(θ/2)Y
Parameters:
Returns: Updated quantum circuit with RY gate appended
Example: (ry-gate (create-circuit 1) 0 3.14) ; π rotation ;=> Circuit with RY(π) gate on qubit 0
Add a rotation around the Y-axis gate to the quantum circuit. The RY gate rotates a qubit around the Y-axis of the Bloch sphere by the specified angle. RY(θ) = cos(θ/2)I - i*sin(θ/2)Y Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) - angle: Rotation angle in radians Returns: Updated quantum circuit with RY gate appended Example: (ry-gate (create-circuit 1) 0 3.14) ; π rotation ;=> Circuit with RY(π) gate on qubit 0
(rz-gate circuit target angle)
Add a rotation around the Z-axis gate to the quantum circuit.
The RZ gate rotates a qubit around the Z-axis of the Bloch sphere by the specified angle. RZ(θ) = cos(θ/2)I - i*sin(θ/2)Z
Parameters:
Returns: Updated quantum circuit with RZ gate appended
Example: (rz-gate (create-circuit 1) 0 0.785) ; π/4 rotation ;=> Circuit with RZ(π/4) gate on qubit 0
Add a rotation around the Z-axis gate to the quantum circuit. The RZ gate rotates a qubit around the Z-axis of the Bloch sphere by the specified angle. RZ(θ) = cos(θ/2)I - i*sin(θ/2)Z Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) - angle: Rotation angle in radians Returns: Updated quantum circuit with RZ gate appended Example: (rz-gate (create-circuit 1) 0 0.785) ; π/4 rotation ;=> Circuit with RZ(π/4) gate on qubit 0
(s-dag-gate circuit target)
Add an S-dagger (S†) gate to the quantum circuit.
The S† gate applies a phase of -π/2: |0⟩ → |0⟩ and |1⟩ → -i|1⟩. It's equivalent to a Z^(-1/2) gate and is the inverse of the S gate.
Parameters:
Returns: Updated quantum circuit with S† gate appended
Example: (s-dag-gate (create-circuit 2) 0) ;=> Circuit with S† gate on qubit 0
Add an S-dagger (S†) gate to the quantum circuit. The S† gate applies a phase of -π/2: |0⟩ → |0⟩ and |1⟩ → -i|1⟩. It's equivalent to a Z^(-1/2) gate and is the inverse of the S gate. Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with S† gate appended Example: (s-dag-gate (create-circuit 2) 0) ;=> Circuit with S† gate on qubit 0
(s-gate circuit target)
Add an S (phase) gate to the quantum circuit.
The S gate applies a phase of π/2: |0⟩ → |0⟩ and |1⟩ → i|1⟩. It's equivalent to a Z^(1/2) gate.
Parameters:
Returns: Updated quantum circuit with S gate appended
Example: (s-gate (create-circuit 2) 0) ;=> Circuit with S gate on qubit 0
Add an S (phase) gate to the quantum circuit. The S gate applies a phase of π/2: |0⟩ → |0⟩ and |1⟩ → i|1⟩. It's equivalent to a Z^(1/2) gate. Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with S gate appended Example: (s-gate (create-circuit 2) 0) ;=> Circuit with S gate on qubit 0
(swap-gate circuit qubit1 qubit2)
Add a SWAP gate to the quantum circuit.
The SWAP gate exchanges the states of two qubits. It effectively swaps the quantum states stored in the two qubits.
Parameters:
Returns: Updated quantum circuit with SWAP gate appended
Example: (swap-gate (create-circuit 3) 0 2) ;=> Circuit with SWAP gate between qubits 0 and 2
Add a SWAP gate to the quantum circuit. The SWAP gate exchanges the states of two qubits. It effectively swaps the quantum states stored in the two qubits. Parameters: - circuit: Quantum circuit to add the gate to - qubit1: Integer index of the first qubit (0-indexed) - qubit2: Integer index of the second qubit (0-indexed) Returns: Updated quantum circuit with SWAP gate appended Example: (swap-gate (create-circuit 3) 0 2) ;=> Circuit with SWAP gate between qubits 0 and 2
(t-dag-gate circuit target)
Add a T-dagger (T†) gate to the quantum circuit.
The T† gate applies a phase of -π/4: |0⟩ → |0⟩ and |1⟩ → e^(-iπ/4)|1⟩. It's equivalent to a Z^(-1/4) gate and is the inverse of the T gate.
Parameters:
Returns: Updated quantum circuit with T† gate appended
Example: (t-dag-gate (create-circuit 2) 0) ;=> Circuit with T† gate on qubit 0
Add a T-dagger (T†) gate to the quantum circuit. The T† gate applies a phase of -π/4: |0⟩ → |0⟩ and |1⟩ → e^(-iπ/4)|1⟩. It's equivalent to a Z^(-1/4) gate and is the inverse of the T gate. Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with T† gate appended Example: (t-dag-gate (create-circuit 2) 0) ;=> Circuit with T† gate on qubit 0
(t-gate circuit target)
Add a T gate to the quantum circuit.
The T gate applies a phase of π/4: |0⟩ → |0⟩ and |1⟩ → e^(iπ/4)|1⟩. It's equivalent to a Z^(1/4) gate and is important for universal quantum computation.
Parameters:
Returns: Updated quantum circuit with T gate appended
Example: (t-gate (create-circuit 2) 1) ;=> Circuit with T gate on qubit 1
Add a T gate to the quantum circuit. The T gate applies a phase of π/4: |0⟩ → |0⟩ and |1⟩ → e^(iπ/4)|1⟩. It's equivalent to a Z^(1/4) gate and is important for universal quantum computation. Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with T gate appended Example: (t-gate (create-circuit 2) 1) ;=> Circuit with T gate on qubit 1
(toffoli-gate circuit control1 control2 target)
Add a Toffoli (CCNOT) gate to the quantum circuit.
The Toffoli gate is a three-qubit gate that applies an X (NOT) to the target qubit if and only if both control qubits are in state |1⟩. It is also known as the CCX (controlled-controlled-X) gate.
Parameters:
Returns: Updated quantum circuit with Toffoli gate appended
Example: (toffoli-gate (create-circuit 3) 0 1 2) ;=> Circuit with Toffoli gate with controls on qubits 0,1 and target on qubit 2
Add a Toffoli (CCNOT) gate to the quantum circuit. The Toffoli gate is a three-qubit gate that applies an X (NOT) to the target qubit if and only if both control qubits are in state |1⟩. It is also known as the CCX (controlled-controlled-X) gate. Parameters: - circuit: Quantum circuit to add the gate to - control1: Integer index of the first control qubit (0-indexed) - control2: Integer index of the second control qubit (0-indexed) - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with Toffoli gate appended Example: (toffoli-gate (create-circuit 3) 0 1 2) ;=> Circuit with Toffoli gate with controls on qubits 0,1 and target on qubit 2
(x-gate circuit target)
Add a Pauli-X (NOT) gate to the quantum circuit.
The Pauli-X gate flips the state of a qubit: |0⟩ → |1⟩ and |1⟩ → |0⟩. It's equivalent to a classical NOT gate.
Parameters:
Returns: Updated quantum circuit with X gate appended
Example: (x-gate (create-circuit 2) 0) ;=> Circuit with X gate on qubit 0
Add a Pauli-X (NOT) gate to the quantum circuit. The Pauli-X gate flips the state of a qubit: |0⟩ → |1⟩ and |1⟩ → |0⟩. It's equivalent to a classical NOT gate. Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with X gate appended Example: (x-gate (create-circuit 2) 0) ;=> Circuit with X gate on qubit 0
(y-gate circuit target)
Add a Pauli-Y gate to the quantum circuit.
The Pauli-Y gate applies a bit flip and phase flip: |0⟩ → i|1⟩ and |1⟩ → -i|0⟩.
Parameters:
Returns: Updated quantum circuit with Y gate appended
Example: (y-gate (create-circuit 2) 1) ;=> Circuit with Y gate on qubit 1
Add a Pauli-Y gate to the quantum circuit. The Pauli-Y gate applies a bit flip and phase flip: |0⟩ → i|1⟩ and |1⟩ → -i|0⟩. Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with Y gate appended Example: (y-gate (create-circuit 2) 1) ;=> Circuit with Y gate on qubit 1
(z-gate circuit target)
Add a Pauli-Z gate to the quantum circuit.
The Pauli-Z gate applies a phase flip: |0⟩ → |0⟩ and |1⟩ → -|1⟩.
Parameters:
Returns: Updated quantum circuit with Z gate appended
Example: (z-gate (create-circuit 2) 0) ;=> Circuit with Z gate on qubit 0
Add a Pauli-Z gate to the quantum circuit. The Pauli-Z gate applies a phase flip: |0⟩ → |0⟩ and |1⟩ → -|1⟩. Parameters: - circuit: Quantum circuit to add the gate to - target: Integer index of the target qubit (0-indexed) Returns: Updated quantum circuit with Z gate appended Example: (z-gate (create-circuit 2) 0) ;=> Circuit with Z gate on qubit 0
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close