Skip to content

State utility functions for simulator

numqi.sim.state

numqi.sim.state.measure_quantum_vector(q0, index, seed=None)

measure the quantum vector

Parameters:

Name Type Description Default
q0 ndarray

the quantum vector, ndim=1

required
index (int, tuple[int])

the index to measure, must be sorted (ascending)

required
seed (int, None, Generator)

the random seed

None

Returns:

Name Type Description
bitstr list[int]

the measurement result

prob ndarray

the probability of each result

q1 ndarray

the quantum vector after measurement

numqi.sim.state.new_base(num_qubit, dtype=np.complex128)

return the base state of the qubit quantum system

Parameters:

Name Type Description Default
num_qubit int

the number of qubits

required
dtype dtype

the data type of the base state

complex128

Returns:

Name Type Description
ret ndarray

the base state

numqi.sim.state.reduce_shape_index(shape, index)

reduce the shape and index

  1. group index by None

  2. group index by integer

Parameters:

Name Type Description Default
shape tuple[int]

the shape of the tensor

required
index tuple[int | None]

the index list

required

Returns:

Name Type Description
reduced_shape tuple[int]

the reduced shape

reduced_index tuple[int | None]

the reduced index list

numqi.sim.state.apply_gate(q0, op, index)

apply the gate to the quantum vector

Parameters:

Name Type Description Default
q0 ndarray

the quantum vector, ndim=1

required
op ndarray

the gate, ndim=2

required
index (int, tuple[int])

the index of the qubits to apply the gate, count from left to right |0123>

required

Returns:

Name Type Description
ret ndarray

the quantum vector after applying the gate

numqi.sim.state.apply_gate_grad(q0_conj, q0_grad, op, index, tag_op_grad=True)

gradient back propagation of apply_gate

Parameters:

Name Type Description Default
q0_conj ndarray

the conjugate of the quantum vector, ndim=1

required
q0_grad ndarray

the gradient of the quantum vector, ndim=1

required
op ndarray

the gate, ndim=2

required
index (int, tuple[int])

the index of the qubits to apply the gate

required
tag_op_grad bool

whether to calculate the gradient of the gate

True

Returns:

Name Type Description
q0_conj ndarray

the conjugate of the quantum vector before applying the gate

q0_grad ndarray

the gradient of the quantum vector before applying the gate

op_grad (ndarray, None)

the gradient of the gate, None if tag_op_grad=False

numqi.sim.state.apply_control_n_gate(q0, op, ind_control_set, ind_target)

apply the n-controlled gate to the quantum vector

Parameters:

Name Type Description Default
q0 ndarray

the quantum vector, ndim=1

required
op ndarray

the gate, ndim=2

required
ind_control_set (int, set[int])

the index of the control qubits

required
ind_target (int, tuple[int])

the index of the target qubits

required

Returns:

Name Type Description
ret ndarray

the quantum vector after applying the gate

numqi.sim.state.apply_control_n_gate_grad(q0_conj, q0_grad, op, ind_control_set, ind_target, tag_op_grad=True)

gradient back propagation of apply_control_n_gate

Parameters:

Name Type Description Default
q0_conj ndarray

the conjugate of the quantum vector, ndim=1

required
q0_grad ndarray

the gradient of the quantum vector, ndim=1

required
op ndarray

the gate, ndim=2

required
ind_control_set (int, set[int])

the index of the control qubits

required
ind_target (int, tuple[int])

the index of the target qubits

required
tag_op_grad bool

whether to calculate the gradient of the gate

True

Returns:

Name Type Description
q0_conj ndarray

the conjugate of the quantum vector before applying the gate

q0_grad ndarray

the gradient of the quantum vector before applying the gate

op_grad (ndarray, None)

the gradient of the gate, None if tag_op_grad=False

numqi.sim.state.inner_product_psi0_O_psi1(psi0, psi1, op_list)

calculate the inner product of

Parameters:

Name Type Description Default
psi0 ndarray

the quantum vector, ndim=1

required
psi1 ndarray

the quantum vector, ndim=1

required
op_list list[list[tuple]]

the operator list. The first level of list is the sum of the operator and the second level of list is matrix multiplication (from left to right). Each tuple is a gate and the index

required

Returns:

Name Type Description
ret ndarray

the inner product, ndim=1 of the length equal to the number of operators (first level of list)

numqi.sim.state.reduce_to_probability(q0, keep_index_set)

reduce the quantum vector to the probability

Parameters:

Name Type Description Default
q0 ndarray

the quantum vector, ndim=1

required
keep_index_set set[int]

the index to keep

required

Returns:

Name Type Description
ret ndarray

the probability