Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Qiskit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ include("c_circuit.jl")
include("c_target.jl")
include("c_transpile.jl")
include("c_observable.jl")
include("c_param.jl")

end # module C

import .C: LibQiskit

include("parameter.jl")
include("circuit.jl")
include("target.jl")
include("transpile.jl")
Expand Down
18 changes: 17 additions & 1 deletion src/c_circuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ function qk_circuit_gate(qc::Ref{QkCircuit}, gate::QkGate, qubits::AbstractVecto
nothing
end

function qk_circuit_parameterized_gate(qc::Ref{QkCircuit}, gate::QkGate, qubits::AbstractVector{<:Integer}, params::AbstractVector{Ptr{QkParam}}; offset::Int = 1)::Nothing
check_not_null(qc)
if length(qubits) != qk_gate_num_qubits(gate)
throw(ArgumentError("Unexpected number of qubits for gate."))
end
if length(params) != qk_gate_num_params(gate)
throw(ArgumentError("Unexpected number of parameters for gate."))
end
if !checkindex(Bool, range(offset, length=qk_circuit_num_qubits(qc)), qubits)
throw(ArgumentError("Invalid qubit index"))
end
qubits0 = Vector{UInt32}(qubits .- offset)
check_exit_code(LibQiskit.qk_circuit_parameterized_gate(qc, gate, qubits0, params))
nothing
end

function qk_circuit_measure(qc::Ref{QkCircuit}, qubit::Integer, clbit::Integer; offset::Int = 1)::Nothing
check_not_null(qc)
if !checkindex(Bool, range(offset, length=qk_circuit_num_qubits(qc)), qubit)
Expand Down Expand Up @@ -187,7 +203,7 @@ end

export QkGate, QkCircuit, QkDelayUnit, QkParam
export qk_circuit_free, qk_circuit_num_qubits, qk_circuit_num_clbits, qk_circuit_num_instructions, qk_circuit_get_instruction, qk_circuit_count_ops
export qk_circuit_gate, qk_circuit_measure, qk_circuit_reset, qk_circuit_barrier, qk_circuit_unitary, qk_circuit_delay
export qk_circuit_gate, qk_circuit_parameterized_gate, qk_circuit_measure, qk_circuit_reset, qk_circuit_barrier, qk_circuit_unitary, qk_circuit_delay

# Export enum instances
for e in (QkGate, QkDelayUnit)
Expand Down
165 changes: 165 additions & 0 deletions src/c_param.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2025.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

import .LibQiskit: QkParam

function qk_param_free(param::Ptr{QkParam})::Nothing
LibQiskit.qk_param_free(param)
nothing
end

function qk_param_new_symbol(name::String)::Ptr{QkParam}
ptr = LibQiskit.qk_param_new_symbol(name)
if ptr == C_NULL
throw(ErrorException("Failed to create parameter symbol."))
end
ptr
end

function qk_param_from_double(value::Float64)::Ptr{QkParam}
ptr = LibQiskit.qk_param_from_double(value)
if ptr == C_NULL
throw(ErrorException("Failed to create parameter from double."))
end
ptr
end

function qk_param_from_complex(value::ComplexF64)::Ptr{QkParam}
ptr = LibQiskit.qk_param_from_complex(LibQiskit.QkComplex64(real(value), imag(value)))
if ptr == C_NULL
throw(ErrorException("Failed to create parameter from complex."))
end
ptr
end

function qk_param_zero()::Ptr{QkParam}
ptr = LibQiskit.qk_param_zero()
if ptr == C_NULL
throw(ErrorException("Failed to create zero parameter."))
end
ptr
end

function qk_param_copy(param::Ptr{QkParam})::Ptr{QkParam}
ptr = LibQiskit.qk_param_copy(param)
if ptr == C_NULL
throw(ErrorException("Failed to copy parameter."))
end
ptr
end

function qk_param_str(param::Ptr{QkParam})::String
cstr = LibQiskit.qk_param_str(param)
s = unsafe_string(cstr)
LibQiskit.qk_str_free(cstr)
s
end

function qk_param_equal(a::Ptr{QkParam}, b::Ptr{QkParam})::Bool
LibQiskit.qk_param_equal(a, b)
end

function qk_param_as_real(param::Ptr{QkParam})::Float64
LibQiskit.qk_param_as_real(param)
end

function qk_param_add(out::Ptr{QkParam}, a::Ptr{QkParam}, b::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_add(out, a, b))
nothing
end

function qk_param_sub(out::Ptr{QkParam}, a::Ptr{QkParam}, b::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_sub(out, a, b))
nothing
end

function qk_param_mul(out::Ptr{QkParam}, a::Ptr{QkParam}, b::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_mul(out, a, b))
nothing
end

function qk_param_div(out::Ptr{QkParam}, num::Ptr{QkParam}, den::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_div(out, num, den))
nothing
end

function qk_param_pow(out::Ptr{QkParam}, base::Ptr{QkParam}, pow::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_pow(out, base, pow))
nothing
end

function qk_param_sin(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_sin(out, src))
nothing
end

function qk_param_cos(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_cos(out, src))
nothing
end

function qk_param_tan(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_tan(out, src))
nothing
end

function qk_param_asin(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_asin(out, src))
nothing
end

function qk_param_acos(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_acos(out, src))
nothing
end

function qk_param_atan(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_atan(out, src))
nothing
end

function qk_param_log(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_log(out, src))
nothing
end

function qk_param_exp(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_exp(out, src))
nothing
end

function qk_param_abs(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_abs(out, src))
nothing
end

function qk_param_sign(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_sign(out, src))
nothing
end

function qk_param_neg(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_neg(out, src))
nothing
end

function qk_param_conjugate(out::Ptr{QkParam}, src::Ptr{QkParam})::Nothing
check_exit_code(LibQiskit.qk_param_conjugate(out, src))
nothing
end

export QkParam
export qk_param_free, qk_param_new_symbol, qk_param_from_double, qk_param_from_complex
export qk_param_zero, qk_param_copy, qk_param_str, qk_param_equal, qk_param_as_real
export qk_param_add, qk_param_sub, qk_param_mul, qk_param_div, qk_param_pow
export qk_param_sin, qk_param_cos, qk_param_tan, qk_param_asin, qk_param_acos, qk_param_atan
export qk_param_log, qk_param_exp, qk_param_abs, qk_param_sign, qk_param_neg, qk_param_conjugate
17 changes: 14 additions & 3 deletions src/circuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# that they have been altered from the originals.

import .C: qk_circuit_free, qk_circuit_num_qubits, qk_circuit_num_clbits, qk_circuit_num_instructions, qk_circuit_get_instruction, qk_circuit_count_ops, QkCircuit, QkGate, CircuitInstruction, QkDelayUnit, QkDelayUnit_S, QkDelayUnit_MS, QkDelayUnit_US, QkDelayUnit_NS, QkDelayUnit_PS
import .C: qk_circuit_gate, qk_circuit_measure, qk_circuit_reset, qk_circuit_barrier, qk_circuit_unitary, qk_circuit_delay, check_not_null
import .C: qk_circuit_gate, qk_circuit_parameterized_gate, qk_circuit_measure, qk_circuit_reset, qk_circuit_barrier, qk_circuit_unitary, qk_circuit_delay, check_not_null
using .C

"""
Expand Down Expand Up @@ -105,9 +105,17 @@ function _apply_gate(qc::QuantumCircuit, gate, num_qubits::Int, num_params::Int,
if length(args) != num_qubits + num_params
throw(ArgumentError("Unexpected number of arguments for gate"))
end
params = collect(Float64, args[1:num_params])
param_args = args[1:num_params]
qubits = collect(Int32, args[num_params+1:end])
qk_circuit_gate(qc, gate, qubits, params)
if all(p -> isa(p, Real), param_args)
params = collect(Float64, param_args)
qk_circuit_gate(qc, gate, qubits, params)
elseif all(p -> isa(p, Parameter), param_args)
param_ptrs = Ptr{QkParam}[p.ptr for p in param_args]
qk_circuit_parameterized_gate(qc, gate, qubits, param_ptrs)
else
throw(ArgumentError("Gate parameters must be all real numbers or all Parameter objects"))
end
return nothing
end

Expand Down Expand Up @@ -422,6 +430,9 @@ end
qk_circuit_gate(qc::QuantumCircuit, args...)::Nothing =
qk_circuit_gate(qc.ptr, args...; offset=qc.offset)

qk_circuit_parameterized_gate(qc::QuantumCircuit, args...)::Nothing =
qk_circuit_parameterized_gate(qc.ptr, args...; offset=qc.offset)

qk_circuit_measure(qc::QuantumCircuit, qubit::Integer, clbit::Integer)::Nothing =
qk_circuit_measure(qc.ptr, qubit, clbit; offset=qc.offset)

Expand Down
Loading
Loading