Qiskit: quantum circuit simulation

Circuit


source

get_number_of_gate_params

 get_number_of_gate_params (gate_cls)

source

gate_pool_to_gate_classes

 gate_pool_to_gate_classes (gate_pool:list[qiskit.circuit.gate.Gate])

Creates a vocabulary from a gate pool.


source

instruction_name_to_qiskit_gate

 instruction_name_to_qiskit_gate (name:str)

source

schmidt_rank_vector

 schmidt_rank_vector
                      (densityMatrix:qiskit.quantum_info.states.densitymat
                      rix.DensityMatrix)

Return the SRV of a qi.DensityMatrix.


source

rnd_circuit

 rnd_circuit (num_of_qubits, num_of_gates,
              gate_pool:list[qiskit.circuit.gate.Gate], rng)

Create a random circuit.


source

optimize_circuit

 optimize_circuit (qc:qiskit.circuit.quantumcircuit.QuantumCircuit,
                   gate_pool:list[qiskit.circuit.gate.Gate],
                   optimization_level=2)

Use qiskit.compiler.transpile to optimize a circuit.

gs = [ql.HGate, ql.CXGate, ql.CRXGate]
qc = rnd_circuit(num_of_qubits=3, num_of_gates=8, gate_pool=gs, rng=np.random.default_rng())
svr = schmidt_rank_vector(qi.DensityMatrix(qc))
display(f"Rand: svr={svr}    num_gates={len(qc.data)}", qc.draw('mpl'))

qc = optimize_circuit(qc, gs)
svr = schmidt_rank_vector(qi.DensityMatrix(qc))
display(f"Opti: svr={svr}    num_gates={len(qc.data)}", qc.draw('mpl'))
'Rand: svr=[2, 2, 2]    num_gates=8'

'Opti: svr=[2, 2, 2]    num_gates=8'


source

plot_svr_stat

 plot_svr_stat (num_of_qubits, min_gates, max_gates, gs, samples,
                sort=False, opt=True, rng=Generator(PCG64) at
                0x7F166B5643C0)

SRV distrubtion for random sampling

# gs = [ql.HGate, ql.SGate, ql.TGate, ql.CXGate]  # approx universal set
# gs = [ql.HGate, ql.CCXGate]                     # approx universal set
gs = [ql.HGate, ql.CXGate]

plot_svr_stat(num_of_qubits=3, min_gates=6, max_gates=8, gs=gs, samples=int(1e3),  rng=np.random.default_rng())
[1, 1, 1]: 60.2%
[1, 2, 2]: 11.1%
[2, 2, 1]: 10.1%
[2, 2, 2]: 9.9%
[2, 1, 2]: 8.7%

SVR Test cases

def test_srv(system_dims, init, target):
    vec = qi.Statevector(init, dims=system_dims)
    vec *= 1/np.sqrt(vec.trace())
    srv = schmidt_rank_vector(qi.DensityMatrix(vec)) 
    assert srv == target, f"srv: {srv}"
    print(f"passed test, svr: {srv}")
    display(vec.draw('latex', prefix='|\\psi\\rangle = '))
#---------------- |0+> = |00>+|01>
system_dims = (2,2)
init = np.zeros(np.prod(system_dims), dtype=complex)
init[0] = 1
init[1] = 1
test_srv(system_dims, init, [1, 1])

#----------------Bell, |00>+|11>
system_dims = (2,2)
init = np.zeros(np.prod(system_dims), dtype=complex)
init[0] = 1
init[3] = 1
test_srv(system_dims, init, [2, 2])
  
#----------------GHZ, |000>+|111>
system_dims = (2,2,2)
init = np.zeros(np.prod(system_dims), dtype=complex)
init[0] = 1
init[7] = 1
test_srv(system_dims, init, [2,2,2])
 
#----------------Sym, |000>+|111>+|222>
system_dims = (3,3,3)
init = np.zeros(np.prod(system_dims), dtype=complex)
init[0]  = 1
init[13] = 1
init[26] = 1
test_srv(system_dims, init, [3,3,3])
      
#----------------Wikipedia example, |000>+|101>+|210>+|311>
system_dims = (4,4,4)
init = np.zeros(np.prod(system_dims), dtype=complex)
init[0]  = 1
init[17] = 1
init[36] = 1
init[53] = 1
test_srv(system_dims, init, [2, 2, 4])
passed test, svr: [1, 1]

\[|\psi\rangle = \frac{\sqrt{2}}{2} |00\rangle+\frac{\sqrt{2}}{2} |01\rangle\]

passed test, svr: [2, 2]

\[|\psi\rangle = \frac{\sqrt{2}}{2} |00\rangle+\frac{\sqrt{2}}{2} |11\rangle\]

passed test, svr: [2, 2, 2]

\[|\psi\rangle = \frac{\sqrt{2}}{2} |000\rangle+\frac{\sqrt{2}}{2} |111\rangle\]

passed test, svr: [3, 3, 3]

$$\[\begin{align} |\psi\rangle = \begin{bmatrix} \frac{\sqrt{3}}{3} & 0 & 0 & 0 & \cdots & 0 & 0 & \frac{\sqrt{3}}{3} \\ \end{bmatrix} \\ \text{dims=(3, 3, 3)} \end{align}\]$$

passed test, svr: [2, 2, 4]

$$\[\begin{align} |\psi\rangle = \begin{bmatrix} \frac{1}{2} & 0 & 0 & 0 & \cdots & 0 & 0 & 0 \\ \end{bmatrix} \\ \text{dims=(4, 4, 4)} \end{align}\]$$

Back to top