quairkit.database.random

The library of random data generation functions

quairkit.database.random.random_pauli_str_generator(num_qubits, terms=3)

Generate a random observable in list form.

An observable \(O=0.3X\otimes I\otimes I+0.5Y\otimes I\otimes Z\)’s list form is [[0.3, 'x0'], [0.5, 'y0,z2']]. Such an observable is generated by random_pauli_str_generator(3, terms=2).

Parameters:
num_qubits : int

Number of qubits.

terms : int | None

Number of terms in the observable. Defaults to 3.

Returns:

The Hamiltonian of randomly generated observable.

Return type:

List

Examples

observable = random_pauli_str_generator(num_qubits=2, terms=2)
print(f'The Hamiltonian of randomly generated observable is:\n{observable}')
The Hamiltonian of randomly generated observable is:
[[-0.6019637631250563, 'y0'], [0.12777564473712655, 'x0,z1']]
quairkit.database.random.random_state(num_systems, rank=None, is_real=False, size=1, system_dim=2)

Generate a random quantum state.

Parameters:
num_systems : int

The number of qubits contained in the quantum state.

rank : int | None

The rank of the density matrix. Defaults to None (full rank).

is_real : bool | None

If the quantum state only contains real numbers. Defaults to False.

size : int | List[int] | None

Batch size. Defaults to 1.

system_dim : List[int] | int

Dimension of systems. Can be a list of system dimensions or an int representing the dimension of all systems. Defaults to the qubit case.

Raises:

NotImplementedError – If the backend is wrong or not implemented.

Returns:

The generated quantum state.

Return type:

State

Examples

state = random_state(num_systems=1, size=1)
print(f'The generated quantum state is:\n{state}')

state = random_state(num_systems=1, rank=1)
print(f'The generated quantum state is:\n{state}')

state = random_state(num_systems=2, system_dim=[1, 2])
print(f'The generated quantum state is:\n{state}')

state = random_state(num_systems=3, size=[2, 1], system_dim=[1, 2, 1])
print(f'The generated quantum state is:\n{state}')
The generated quantum state is:
---------------------------------------------------
Backend: state_vector
System dimension: [2]
System sequence: [0]
[-0.33+0.72j -0.28+0.55j]
---------------------------------------------------
The generated quantum state is:
---------------------------------------------------
Backend: state_vector
System dimension: [2]
System sequence: [0]
[ 0.83-0.31j -0.45+0.16j]
---------------------------------------------------
The generated quantum state is:
---------------------------------------------------
Backend: density_matrix
System dimension: [1, 2]
System sequence: [0, 1]
[[ 0.63+0.j   -0.12-0.13j]
 [-0.12+0.13j  0.37-0.j  ]]
---------------------------------------------------
The generated quantum state is:
---------------------------------------------------
Backend: density_matrix
System dimension: [1, 2, 1]
System sequence: [0, 1, 2]
Batch size: [2, 1]
# 0:
[[0.43+0.j   0.24-0.37j],
 [0.24+0.37j 0.57-0.j  ]]
# 1:
[[ 0.88+0.j   -0.29-0.11j],
 [-0.29+0.11j  0.12-0.j  ]]
---------------------------------------------------
quairkit.database.random.random_hamiltonian_generator(num_qubits, terms=3)

Generate a random Hamiltonian.

Parameters:
num_qubits : int

Number of qubits.

terms : int | None

Number of terms in the Hamiltonian. Defaults to 3.

Returns:

The randomly generated Hamiltonian.

Return type:

Hamiltonian

Examples

Hamiltonian = random_hamiltonian_generator(3, 2)
print(f'The randomly generated Hamiltonian is:\n{Hamiltonian}')
The randomly generated Hamiltonian is:
0.11801365595625102 Z0, X2
0.9059897222160238 X0, Z1, Z2
quairkit.database.random.random_hermitian(num_qubits)

Randomly generate a normalized \(2^n \times 2^n\) Hermitian matrix.

Parameters:
num_qubits : int

Number of qubits \(n\).

Returns:

A \(2^n \times 2^n\) Hermitian matrix.

Return type:

Tensor

Examples

Hermitian = random_hermitian(1)
print(f'The randomly generated Hermitian matrix is:\n{Hermitian}')
The randomly generated Hermitian matrix is:
tensor([[0.1038+0.0000j, 0.4251+0.2414j],
        [0.4251-0.2414j, 0.7333+0.0000j]])
quairkit.database.random.random_projector(num_systems, size=1, system_dim=2)

Randomly generate a \(d \times d\) orthogonal projector.

Parameters:
num_systems : int

Number of systems.

size : int

Number of projectors.

system_dim : List[int] | int

Dimension of systems. Can be a list of system dimensions or an int representing the dimension of all systems. Defaults to the qubit case.

Returns:

A \(2^n \times 2^n\) orthogonal projector.

Return type:

Tensor

Examples

Orthogonal_projector = random_orthogonal_projection(1)
print(f'The randomly generated Orthogonal_projector is:\n{Orthogonal_projector}')
The randomly generated Orthogonal_projector is:
tensor([[0.9704+1.1123e-10j, 0.1692+7.7007e-03j],
        [0.1692-7.7008e-03j, 0.0296-1.1123e-10j]])
quairkit.database.random.random_orthogonal_projection(num_systems, size=1, system_dim=2)

Randomly generate a \(d \times d\) orthogonal projector.

Parameters:
num_systems : int

Number of systems.

size : int

Number of projectors.

system_dim : List[int] | int

Dimension of systems. Can be a list of system dimensions or an int representing the dimension of all systems. Defaults to the qubit case.

Returns:

A \(2^n \times 2^n\) orthogonal projector.

Return type:

Tensor

Examples

Orthogonal_projector = random_orthogonal_projection(1)
print(f'The randomly generated Orthogonal_projector is:\n{Orthogonal_projector}')
The randomly generated Orthogonal_projector is:
tensor([[0.9704+1.1123e-10j, 0.1692+7.7007e-03j],
        [0.1692-7.7008e-03j, 0.0296-1.1123e-10j]])
quairkit.database.random.random_density_matrix(num_qubits)

Randomly generate an num_qubits-qubit state in density matrix form.

Parameters:
num_qubits : int

Number of qubits \(n\).

Returns:

A \(2^n \times 2^n\) density matrix.

Return type:

Tensor

Examples

Density_matrix = random_density_matrix(1)
print(f'The randomly generated density matrix is:\n{Density_matrix}')
The randomly generated density matrix is:
tensor([[0.3380+0.0000j, 0.4579+0.1185j],
        [0.4579-0.1185j, 0.6620+0.0000j]])
quairkit.database.random.random_unitary(num_systems, size=1, system_dim=2)

Randomly generate a \(d \times d\) unitary.

Parameters:
num_systems : int

Number of systems in this unitary (alias of num_qubits).

size : int | List[int]

Batch size. Defaults to 1.

system_dim : List[int] | int

Dimension of systems. Can be a list of system dimensions or an int representing the dimension of all systems. Defaults to the qubit case.

Returns:

A \(d \times d\) unitary matrix.

Return type:

Tensor

Examples

unitary_matrix_1 = random_unitary(num_systems=1, system_dim=2)
print(f'The randomly generated unitary_matrix_1 is:\n{unitary_matrix_1}')

unitary_matrix_2 = random_unitary(num_systems=2, system_dim=[1, 2])
print(f'The randomly generated unitary_matrix_2 is:\n{unitary_matrix_2}')

unitary_matrix_3 = random_unitary(num_systems=1, size=[1,2])
print(f'The randomly generated unitary_matrix_3 is:\n{unitary_matrix_3}')
The randomly generated unitary_matrix_1 is:
tensor([[-0.5288+0.5296j, -0.5277-0.4019j],
        [-0.5321-0.3959j, -0.3627+0.6546j]])
The randomly generated unitary_matrix_2 is:
tensor([[0.6996-0.1504j, 0.3414+0.6095j],
        [0.4954-0.4925j, -0.6315-0.3364j]])
The randomly generated unitary_matrix_3 is:
tensor([[[[0.3240+0.1404j, 0.4166-0.8377j],
          [0.6068+0.7121j, -0.2804+0.2146j]],

         [[-0.2620-0.0886j, -0.3587+0.8916j],
          [0.5196-0.8084j, 0.2238+0.1624j]]]])
quairkit.database.random.random_unitary_hermitian(num_qubits)

Randomly generate a \(2^n \times 2^n\) Hermitian unitary.

Parameters:
num_qubits : int

Number of qubits \(n\).

Returns:

A \(2^n \times 2^n\) Hermitian unitary matrix.

Return type:

Tensor

Examples

Unitary_hermitian = random_unitary_hermitian(1)
print(f'The randomly generated hermitian unitary is:\n{Unitary_hermitian}')
The randomly generated hermitian unitary is:
tensor([[0.2298+2.2018e-09j, -0.8408+4.9013e-01j],
        [-0.8408-4.9013e-01j, -0.2298-2.2018e-09j]])
quairkit.database.random.random_unitary_with_hermitian_block(num_qubits, is_unitary=False)

Randomly generate a unitary \(2^n \times 2^n\) matrix that is a block encoding of a \(2^{n/2} \times 2^{n/2}\) Hermitian matrix.

Parameters:
num_qubits : int

Number of qubits \(n\).

is_unitary : bool

Whether the Hermitian block is a unitary divided by 2 (for tutorial only).

Returns:

A \(2^n \times 2^n\) unitary matrix whose upper-left block is Hermitian.

Return type:

Tensor

Examples

unitary_matrix_1 = random_unitary_with_hermitian_block(num_qubits=2, is_unitary=False)
print(f'The randomly generated unitary matrix 1 with hermitian block is:\n{unitary_matrix_1}')

unitary_matrix_2 = random_unitary_with_hermitian_block(num_qubits=2, is_unitary=True)
print(f'The randomly generated unitary matrix 2 with hermitian block is:\n{unitary_matrix_2}')
The randomly generated unitary matrix 1 with hermitian block is:
tensor([[ 5.7873e-01+0.0000j,  2.2460e-01+0.2711j, -1.5514e-08+0.5646j,
          3.6316e-01-0.3009j],
        [ 2.2460e-01-0.2711j,  7.0578e-01+0.0000j, -3.6316e-01-0.3009j,
         -2.2489e-08+0.3944j],
        [-1.5514e-08+0.5646j,  3.6316e-01-0.3009j,  5.7873e-01+0.0000j,
          2.2460e-01+0.2711j],
        [-3.6316e-01-0.3009j, -2.2489e-08+0.3944j,  2.2460e-01-0.2711j,
          7.0578e-01+0.0000j]])
The randomly generated unitary matrix 2 with hermitian block is:
tensor([[-1.8185e-01-1.6847e-09j,  3.0894e-01+3.4855e-01j,
         2.2516e-09+8.6603e-01j, -1.4451e-09-9.3500e-11j],
        [ 3.0894e-01-3.4855e-01j,  1.8185e-01+1.6847e-09j,
         1.7456e-09-9.3500e-11j,  2.4038e-09+8.6603e-01j],
        [ 2.2516e-09+8.6603e-01j, -1.4451e-09-9.3500e-11j,
        -1.8185e-01-1.6847e-09j,  3.0894e-01+3.4855e-01j],
        [ 1.7456e-09-9.3500e-11j,  2.4038e-09+8.6603e-01j,
         3.0894e-01-3.4855e-01j,  1.8185e-01+1.6847e-09j]])
quairkit.database.random.haar_orthogonal(dim)

Randomly generate an orthogonal matrix following Haar measure, referenced by arXiv:math-ph/0609050v2.

Parameters:
dim : int

Dimension of the orthogonal matrix.

Returns:

A \(d \times d\) orthogonal matrix.

Return type:

Tensor

Examples

Haar_orthogonal = haar_orthogonal(2)
print(f'The randomly generated orthogonal matrix is:\n{Haar_orthogonal}')
The randomly generated orthogonal matrix is:
tensor([[-0.6859+0.j,  0.7277+0.j],
        [ 0.7277+0.j,  0.6859+0.j]])
quairkit.database.random.haar_unitary(dim)

Randomly generate a unitary following Haar measure, referenced by arXiv:math-ph/0609050v2.

Parameters:
dim : int

Dimension of the unitary.

Returns:

A \(d \times d\) unitary.

Return type:

Tensor

Examples

Haar_unitary = haar_unitary(2)
print(f'The randomly generated unitary is:\n{Haar_unitary}')
The randomly generated unitary is:
tensor([[ 0.2800+0.6235j,  0.7298+0.0160j],
        [-0.7289-0.0396j,  0.3267-0.6003j]])
quairkit.database.random.haar_state_vector(dim, is_real=False)

Randomly generate a state vector following Haar measure.

Parameters:
dim : int

Dimension of the state vector.

is_real : bool | None

Whether the vector is real. Defaults to False.

Returns:

A \(d \times 1\) state vector.

Return type:

Tensor

Examples

Haar_state_vector = haar_state_vector(3, is_real=True)
print(f'The randomly generated state vector is:\n{Haar_state_vector}')
The randomly generated state vector is:
tensor([[0.9908+0.j],
        [-0.1356+0.j]])
quairkit.database.random.haar_density_operator(dim, rank, is_real=False)

Randomly generate a density matrix following Haar measure.

Parameters:
dim : int

Dimension of the density matrix.

rank : int

Rank of the density matrix.

is_real : bool | None

Whether the density matrix is real. Defaults to False.

Returns:

A \(d \times d\) density matrix.

Return type:

Tensor

Examples

rho1 = haar_density_operator(dim=2, rank=2)
print(f'The randomly generated density matrix 1 is:\n{rho1}')

rho2 = haar_density_operator(dim=2, rank=1, is_real=True)
print(f'The randomly generated density matrix 2 is:\n{rho2}')
The randomly generated density matrix 1 is:
tensor([[0.8296+1.1215e-18j, -0.0430+3.5193e-01j],
        [-0.0430-3.5193e-01j, 0.1704-1.1215e-18j]])
The randomly generated density matrix 2 is:
tensor([[0.6113+0.j, -0.4875+0.j],
        [-0.4875+0.j, 0.3887+0.j]])
quairkit.database.random.random_channel(num_systems, rank=None, target='kraus', size=1, system_dim=2)

Generate a random channel from its Stinespring representation.

Parameters:
num_systems : int

Number of systems.

rank : int

Rank of this channel. If None, it is randomly sampled from \([1, d]\).

target : str

Target representation; should be 'choi', 'kraus' or 'stinespring'.

size : int | None

Batch size. Defaults to 1.

system_dim : List[int] | int

Dimension of systems. Can be a list of system dimensions or an int representing the dimension of all systems. Defaults to the qubit case.

Returns:

The target representation of a random channel.

Return type:

Tensor

Examples

channel_kraus = random_channel(num_systems=1, target="kraus", system_dim=2)
print(f'The randomly generated kraus channel is:\n{channel_kraus}')

channel_choi = random_channel(num_systems=1, target="choi", system_dim=2)
print(f'The randomly generated choi channel is:\n{channel_choi}')

channel_stinespring = random_channel(num_systems=1, target="stinespring", size=1, rank=1)
print(f'The randomly generated stinespring channel is:\n{channel_stinespring}')

batch_channels = random_channel(num_systems=2, size=2, target="kraus", system_dim=[1,2], rank=2)
print(f'The randomly generated kraus channel is:\n{batch_channels}')
The randomly generated kraus channel is:
tensor([[[ 0.2361+0.7497j,  0.0224+0.6178j],
         [ 0.5942-0.1706j, -0.7860+0.0085j]]])
The randomly generated choi channel is:
tensor([[ 0.5136+0.0000j, -0.4784-0.1446j, -0.2850-0.4106j, -0.1583-0.4886j],
        [-0.4784+0.1446j,  0.4864+0.0000j,  0.3811+0.3023j,  0.2850+0.4106j],
        [-0.2850+0.4106j,  0.3811-0.3023j,  0.4864+0.0000j,  0.4784+0.1446j],
        [-0.1583+0.4886j,  0.2850-0.4106j,  0.4784-0.1446j,  0.5136+0.0000j]])
The randomly generated stinespring channel is:
tensor([[ 0.1652+0.5347j,  0.6310+0.5372j],
        [ 0.4751+0.6790j, -0.5167-0.2150j]])
The randomly generated kraus channel is:
tensor([[[[-0.3189-0.6385j, -0.1873+0.1634j],
          [ 0.2218+0.0440j, -0.6294+0.0947j]],

         [[-0.0072+0.0749j, -0.0790+0.6740j],
          [-0.5121-0.4142j, -0.0357-0.2671j]]],


        [[[-0.1163-0.1931j,  0.2001-0.5852j],
          [-0.5174+0.5253j,  0.1128-0.2459j]],

         [[-0.1362+0.3280j, -0.2677+0.5444j],
          [ 0.4328-0.3033j, -0.3862-0.1646j]]]])
quairkit.database.random.random_clifford(num_qubits)

Generate a random Clifford unitary.

Parameters:
num_qubits : int

The number of qubits (n).

Returns:

The matrix form of a random Clifford unitary.

Return type:

Tensor

Reference:

Sergey Bravyi and Dmitri Maslov, Hadamard-free circuits expose the structure of the Clifford group. IEEE Transactions on Information Theory 67(7), 4546-4563 (2021).

Examples

num_qubits = 1
clifford_matrix = random_clifford(num_qubits)
print(f'The randomly generated Clifford unitary is:\n{clifford_matrix}')
The randomly generated Clifford unitary is:
tensor([[ 0.7071+0.0000j, -0.7071+0.0000j],
        [ 0.0000+0.7071j,  0.0000+0.7071j]])