Jawadkotaich/quanbench-plus huggingface dataset:
import pennylane as qml
import numpy as np
def shor_7mod15():
    """
        Design a quantum circuit that implentment a shor's algorithm to solve 7 mod 15, Use a 8 qubits for the phase register and 4 qubits for the modular exponentiation.
        return qml.sample() with number of shots = 1000 phase register.
    """

Framework: pennylane | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit
import numpy as np
from qiskit.circuit.library import QFT
def shor_7mod15()->QuantumCircuit:
    N = 15
    m = int( np.ceil( np.log2( N ) ) )
    phase_register_size = 8
    cu_register_size = 4
    qc = QuantumCircuit(phase_register_size + cu_register_size, phase_register_size)
    qc.h(range(8))
    qc.x(8)

    def cU_multi(k):
        U_qc = QuantumCircuit( m )
        U_qc.x( range(m) )
        U_qc.swap(1, 2)
        U_qc.swap(2, 3)
        U_qc.swap(0, 3)
        U = U_qc.to_gate()
        sys_register_size = 4
        circ = QuantumCircuit( sys_register_size )
        for _ in range(2**k):
            circ.append(U, range(sys_register_size))
        U_multi = circ.to_gate()
        U_multi.name = "7Mod15_[2^{}]".format(k)
        cU_multi = U_multi.control()
        return cU_multi
    
    # def apply_qft_inverse(n):
    #     qc = 
    for k in range( phase_register_size ):
        cU = cU_multi(k)
        qc.append( cU, [k] + list( range( phase_register_size,
                                                ( phase_register_size + cu_register_size ) ) ) )

    # for qubit_idx in range( phase_register_size // 2 ):
    #     qc.swap( qubit_idx, ( 8 - qubit_idx - 1 ) )
    qc.append(QFT(phase_register_size).inverse(), range(phase_register_size))
    qc.measure(range( phase_register_size ),range( phase_register_size ))

    return qc
import pennylane as qml
import numpy as np
def ipe_s_gate() :
    """
        Implement an Iterative Phase Estimation (IPE) circuit to estimate the phase of the S gate, using 2 iterations.
        return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit_aer import AerSimulator
import numpy as np
def ipe_s_gate() -> QuantumCircuit:
    qr = QuantumRegister(2, 'q')
    cr = ClassicalRegister(2, 'c')
    qc = QuantumCircuit(qr, cr)
    qc.h(0)
    qc.x(1)
    qc.cp(np.pi, 0, 1)
    qc.h(0)
    qc.measure(0, 0)
    qc.reset(0)
    qc.h(0)
    with qc.if_test((cr[0], True)):
        qc.p( -np.pi / 2, 0 )
    qc.cp(np.pi/2, 0 ,1)
    qc.h(0)
    qc.measure(0, 1)
    return qc

if __name__ == "__main__":
    # 创建一个模拟器
    simulator = AerSimulator()

    # 执行电路并获取结果
    circuit = ipe_s_gate()
    print(circuit)
    result = simulator.run(circuit).result()
    counts = result.get_counts(circuit)

    # 打印结果
    print("Counts:", counts)
import pennylane as qml
import numpy as np
def ipe_t_gate() :
    """
        Implement an Iterative Phase Estimation (IPE) circuit to estimate the phase of the T gate, using 3 iterations.
        return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
import numpy as np
from qiskit_aer import AerSimulator
def ipe_t_gate() -> QuantumCircuit:
    qr = QuantumRegister(2)
    cr = ClassicalRegister(3)
    qc = QuantumCircuit(qr, cr)
    qc.h(0)
    qc.x(1)
    qc.cp(np.pi,0,1)
    qc.h(0)
    qc.measure(0,0)
    qc.reset(0)
    qc.h(0)
    with qc.if_test( ( cr[0], True ) ): 
        qc.p( -np.pi / 2, 0 )
    qc.cp(np.pi/2,0,1)
    qc.h(0)
    qc.measure(0,1)
    qc.reset(0)
    qc.h(0)
    with qc.if_test( ( cr[0], True ) ): 
        qc.p( -np.pi / 4, 0 )
    qc.cp(np.pi/4,0,1)
    qc.h(0)
    qc.measure(0,2)
    return qc

# if __name__ == "__main__":
#     # 创建一个模拟器
#     simulator = AerSimulator()

#     # 执行电路并获取结果
#     circuit = ipe_t_gate()
#     result = simulator.run(circuit).result()
#     counts = result.get_counts(circuit)

#     # 打印结果
#     print("Counts:", counts)
import pennylane as qml
def parity_check_3bit() :
    """
        Construct a quantum circuit to check the parity of a 3-qubit input state and return qml.sample() with number of shots = 1000
    """

Framework: pennylane | Category: STATE_PREPARATION

Solution:
from qiskit import QuantumCircuit
def parity_check_3bit() -> QuantumCircuit:
    qc = QuantumCircuit(4, 1)
    qc.cx(0, 3)
    qc.cx(1, 3)
    qc.cx(2, 3)
    qc.measure(3, 0)
    return qc
import pennylane as qml
def reverse_state_preparation_bell() :
    """
        Build a circuit to uncompute the Bell state back to |00>. return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: STATE_PREPARATION

Solution:
from qiskit import QuantumCircuit
def reverse_state_preparation_bell() -> QuantumCircuit:
    qc = QuantumCircuit(2, 2)
    qc.cx(0, 1)
    qc.h(0)
    qc.measure(0, 0)
    qc.measure(1, 1)
    return qc
import pennylane as qml
import numpy as np
def controlled_hadamard() :
    """
        Decompose a controlled-Hadamard gate using basic gates. return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: DECOMPOSITION

Solution:
from qiskit import QuantumCircuit
import numpy as np
def controlled_hadamard() -> QuantumCircuit:
    qc = QuantumCircuit(2, 2)
    qc.ry(np.pi/4, 1)
    qc.cx(0, 1)
    qc.ry(-np.pi/4, 1)
    qc.measure(0, 0)
    qc.measure(1, 1)
    return qc
import pennylane as qml
def quantum_state_preparation(parameters=None) :
    """
        Prepares a single-qubit variational quantum state based on input parameters. This function constructs a parameterized ansatz circuit consisting of RX and RY rotationson a single qubit.
        return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: STATE_PREPARATION

Solution:
from qiskit import QuantumCircuit
from qiskit.circuit import ParameterVector
from qiskit.quantum_info import SparsePauliOp
def quantum_state_preparation(parameters) -> QuantumCircuit:
    circuit = QuantumCircuit(1)
    circuit.rx(parameters[0], 0) 
    circuit.ry(parameters[1], 0)
    return circuit
import pennylane as qml
import numpy as np
def VQE_2(parameters=None) :
    """
    Prepares a double-qubit variational quantum state based on input parameters. This function constructs a parameterized ansatz circuit to find the eigenvalue of the observable.
    Args:
        parameters (list or array-like): A list of 8 real-valued parameters:
            - parameters[0–2]: RZ, RZ, RY rotations on qubit 0
            - parameters[3]:    RZ rotation on qubit 1
            - parameters[4–6]: RZ, RZ, RY on qubit 0
            - parameters[7]:    RZ rotation on qubit 1
    return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: STATE_PREPARATION

Solution:
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.circuit.library import U3Gate
from qiskit.circuit import ParameterVector
import numpy as np
def VQE_2(parameters: ParameterVector) -> QuantumCircuit:
    qr = QuantumRegister(2)
    circuit = QuantumCircuit(qr)
    circuit.append(U3Gate(np.pi,0,np.pi) ,[qr[0]])
    circuit.rz(parameters[0], 0)
    circuit.rz(parameters[1], 0)
    circuit.ry(parameters[2], 0)
    circuit.rz(parameters[3], 1)
    circuit.cx(0, 1)
    circuit.rz(parameters[4], 0)
    circuit.rz(parameters[5], 1)
    circuit.ry(parameters[6], 0)
    circuit.rz(parameters[7], 1)
    return circuit
import pennylane as qml
import numpy as np
from scipy.optimize import minimize

def VQE_Z2(param):
    """
    Implement a quantum circuit compute the minimum eigenvalue of the Z2 Hamiltonian using the Variational Quantum Eigensolver (VQE) algorithm. Param is a list of angles 
    return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: STATE_PREPARATION

Solution:
from qiskit import QuantumCircuit

def VQE_Z2(param):
    qc = QuantumCircuit(2, 2)
    qc.u(param[0], param[1], param[2], 0)
    qc.u(param[3], param[4], param[5], 1)
    return qc
import pennylane as qml
import numpy as np
def U_gate_decompose(theta=None, phi=None, lam=None) :
    """
    Decompose U gate into a sequence of RZ and SX gates, ignore the goble phase, return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: DECOMPOSITION

Solution:
from qiskit import QuantumCircuit
import numpy as np
def U_gate_decompose(theta, phi, lam) -> QuantumCircuit: 
    qc = QuantumCircuit(1)
    qc.rz(lam, 0)
    qc.sx(0)
    qc.rz(theta + np.pi, 0)
    qc.sx(0)
    qc.rz(phi + 3*np.pi, 0)
    return qc
import pennylane as qml
import numpy as np
def Toffoli_gate_decompose() :
    """
    Decompose Toffoli gate into a sequence of RZ, SX and CX gates, ignore the goble phase, return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: DECOMPOSITION

Solution:
from qiskit import QuantumCircuit
import numpy as np
def Toffoli_gate_decompose() -> QuantumCircuit:
    qc = QuantumCircuit(3)
    qc.rz(np.pi/2,2)
    qc.sx(2)
    qc.rz(np.pi/2,2)
    qc.cx(1,2)
    qc.rz(-np.pi/4,2)
    qc.cx(0,2)
    qc.rz(np.pi/4,2)
    qc.cx(1,2)
    qc.rz(np.pi/4,1)
    qc.cx(0,2)
    qc.cx(0,1)
    qc.rz(3*np.pi/4,2)
    qc.rz(np.pi/4,0)
    qc.rz(-np.pi/4,1)
    qc.sx(2)
    qc.cx(0,1)
    qc.rz(np.pi/2,2)
    return qc
import pennylane as qml
import numpy as np
def CX_gate_decompose_Clifford() :
    """
    Decompose CX gate into a sequence of local Clifford gates, return qml.sample() with number of shots = 1000.
    """

Framework: pennylane | Category: DECOMPOSITION

Solution:
from qiskit import QuantumCircuit
import numpy as np
def CX_gate_decompose_Clifford() -> QuantumCircuit:
    qc = QuantumCircuit(2,2)
    qc.sdg(0)
    qc.s(1)
    qc.h(1)
    qc.s(1)
    qc.ecr(0, 1)
    qc.x(0)
    qc.measure([0,1],[0,1])
    return qc