Jawadkotaich/quanbench-plus huggingface dataset:
from qiskit import QuantumCircuit
from numpy import pi

def chsh_circuit(alice: int, bob: int)->QuantumCircuit:
    """ 
        Design a CHSH circuit that takes bits of Alice and Bob as input and return the Quantum Circuit after measuring.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit
from numpy import pi
def chsh_circuit(alice: int, bob: int)->QuantumCircuit:
    qc = QuantumCircuit(2,  2)
    qc.h(0)   
    qc.cx(0, 1)
    qc.barrier()
    if alice == 0:        
        qc.ry(0, 0)
    else:
        qc.ry(-pi / 2, 0)    
        qc.measure(0, 0)
        if bob == 0:       
            qc.ry(-pi / 4, 1)
        else:
            qc.ry(pi / 4, 1)
            qc.measure(1, 1)
    return qc
from qiskit import QuantumCircuit

def or_circuit()->QuantumCircuit:
    """ 
        Design a or circuit that takes Qubit 0 and Qubit 1 as input and Qubit 3 as result, return the Quantum Circuit after measure Qubit 2.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit

def or_circuit()->QuantumCircuit:
    qc = QuantumCircuit(3,1)
    qc.x(0)
    qc.x(1)
    qc.ccx(0, 1, 2)
    qc.x(2)    
    qc.measure(2, 0)
    return qc
from qiskit import QuantumCircuit
import numpy as np
def period_finding_7_15()->QuantumCircuit:
    """
        Design a quantum circuit to solve the Period Finding Problem (with a = 7,  N = 15), return the Quantum Circuit after measuring.
    """

Framework: qiskit | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit
import numpy as np
def period_finding_7_15()->QuantumCircuit:
    N = 15
    m = int( np.ceil( np.log2( N ) ) )
    U_qc = QuantumCircuit(m,m)
    U_qc.x( range(m) )
    U_qc.swap(1, 2)
    U_qc.swap(2, 3)
    U_qc.swap(0, 3)
    U_qc.measure(range(m),range(m))
    return U_qc
from qiskit import QuantumCircuit
import numpy as np
def shor_7mod15()->QuantumCircuit:
    """
        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 the Quantum Circuit after measuring phase register.
    """

Framework: qiskit | 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
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
import numpy as np
def ipe_s_gate() -> QuantumCircuit:
    """
        Implement an Iterative Phase Estimation (IPE) circuit to estimate the phase of the S gate, using 2 iterations.
        Return the QuantumCircuit with measurements.
    """

Framework: qiskit | 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)
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
import numpy as np
def ipe_t_gate() -> QuantumCircuit:
    """
        Implement an Iterative Phase Estimation (IPE) circuit to estimate the phase of the T gate, using 3 iterations.
        Return the QuantumCircuit with measurements.
    """

Framework: qiskit | 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)
from qiskit import QuantumCircuit
def parity_check_3bit() -> QuantumCircuit:
    """
        Construct a quantum circuit to check the parity of a 3-qubit input state and return quantum circuit after measure one qubit
    """

Framework: qiskit | 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
from qiskit import QuantumCircuit
def reverse_state_preparation_bell() -> QuantumCircuit:
    """
        Build a circuit to uncompute the Bell state back to |00>. Return the QuantumCircuit after measurement.
    """

Framework: qiskit | 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
from qiskit import QuantumCircuit
import numpy as np
def controlled_hadamard() -> QuantumCircuit:
    """
        Decompose a controlled-Hadamard gate using basic gates. Return the QuantumCircuit after measure.
    """

Framework: qiskit | 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
from qiskit import QuantumCircuit
from qiskit.circuit import ParameterVector
def quantum_state_preparation(parameters: ParameterVector) -> QuantumCircuit:
    """
        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 the QuantumCircuit without measure.
    """

Framework: qiskit | 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
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) -> QuantumCircuit:
    """
    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 the QuantumCircuit without measure.
    """

Framework: qiskit | 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 numpy as np
from qiskit.circuit import QuantumCircuit
from qiskit_aer.primitives import Estimator
from qiskit.primitives import StatevectorSampler as Sampler
from qiskit.quantum_info import SparsePauliOp
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. 
    Return the QuantumCircuit without measure.
    """

Framework: qiskit | 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
from qiskit import QuantumCircuit
import numpy as np
def U_gate_decompose(theta, phi, lam) -> QuantumCircuit:
    """
    Decompose U gate into a sequence of RZ and SX gates, ignore the goble phase, return the quantum circuit without measure.
    """

Framework: qiskit | 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
from qiskit import QuantumCircuit
import numpy as np
def Toffoli_gate_decompose() -> QuantumCircuit:
    """
    Decompose Toffoli gate into a sequence of RZ, SX and CX gates, ignore the goble phase, return the quantum circuit without measure.
    """

Framework: qiskit | 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
from qiskit import QuantumCircuit
import numpy as np
def CX_gate_decompose_Clifford() -> QuantumCircuit:
    """
    Decompose CX gate into a sequence of local Clifford gates, return the quantum circuit after measure all qubits.
    """

Framework: qiskit | 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
import cirq
def grover_search_oracle_00() :
    """
        Implement a quantum circuit using grover algorithm to find oracle |00> using cirq, return cirq.Circuit after measurement with key='result'.
    """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def grover_search_oracle_00() ->QuantumCircuit:
    
    qr = QuantumRegister(2, 'q')
    cr = ClassicalRegister(2, 'c')
    qc = QuantumCircuit(qr, cr)

    qc.h([0, 1])
    qc.x([0, 1])
    qc.cz(0, 1)
    qc.x([0, 1])
    qc.h([0, 1])
    qc.z([0, 1])
    qc.cz(0, 1)
    qc.x([0, 1])
    qc.h([0, 1])
        
    qc.measure([0, 1], [0, 1])
   
    return qc
import cirq
def generate_quantum_state_qubit3() : 

        """
                Implementation a quantum circuit to create the state ψ = √(1/2)(|011⟩ − |100⟩) using cirq, return cirq.Circuit after measurement with key='result'.
        """

Framework: cirq | Category: STATE_PREPARATION

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

def generate_quantum_state_qubit3() -> QuantumCircuit:
    qc = QuantumCircuit(3,3)   
    qc.h(2)     
    qc.x(1)     
    qc.cx(2,1)  
    qc.cx(1,0)
    qc.z(2) 
    qc.measure([0,1,2], [0,1,2])
    
    return qc
import cirq
def grover_3SAT() : 

        """
                Implement a quantum circuit using grover algorithm to solve the 3 SAT problem with 3-CNF formula {((x1) | (x2) | (x3))
                & ((_not(x1)) | (x2) | (x3))
                & ((_not(x1)) | (_not(x2)) | (_not(x3)))
                & ((_not(x1)) | (_not(x2)) | (x3))
                & ((x1) | (x2) | (_not(x3)))
                & ((_not(x1)) | (x2) | (_not(x3)))} using cirq, return cirq.Circuit after measurement with key='result'.
        """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit.circuit.library import PhaseOracle
from qiskit_algorithms import Grover, AmplificationProblem
from qiskit.primitives import Sampler

def grover_3SAT():
    """
    Solve 3-SAT problem using Grover's Algorithm (compatible with new Qiskit version).
    Formula:
    ((x1) ∨ (x2) ∨ (x3)) ∧
    (¬x1 ∨ x2 ∨ x3) ∧
    (¬x1 ∨ ¬x2 ∨ ¬x3) ∧
    (¬x1 ∨ ¬x2 ∨ x3) ∧
    (x1 ∨ x2 ∨ ¬x3) ∧
    (¬x1 ∨ x2 ∨ ¬x3)
    """
    expression = "(x1 | x2 | x3)&(~x1 | x2 | x3)&(~x1 | ~x2 | ~x3)&(~x1 | ~x2 | x3)&(x1 | x2 | ~x3)&(~x1 | x2 | ~x3)"
    
    oracle = PhaseOracle(expression)
    problem = AmplificationProblem(oracle=oracle)
    grover = Grover(sampler=Sampler(options={"shots": 10000}))
    result = grover.amplify(problem)

    return result.circuit_results[0]
import cirq
def qaoa_maxcut_ansatz(G=None, beta=None, gamma=None) : 

        """
                Implement a quantum circuit using QAOA algorithm to solve the maxcut problem with the graph ([[0,3],[0,4],[1,3],[1,4],[2,3],[2,4]]), return cirq.Circuit after measurement with key='result'.
                 Parameters:
                        G (networkx.Graph): The input graph for MaxCut.
                        beta (List[float]): A list of 5 parameters for the mixing unitaries (RX rotations).
                        gamma (List[float]): A list of 5 parameters for the cost unitaries (ZZ rotations).
                        
        """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

def qaoa_maxcut_ansatz(G, beta, gamma) -> QuantumCircuit:
    
    n = len(G.nodes)
    p = len(beta)
    
    qr = QuantumRegister(n, 'q')
    cr = ClassicalRegister(n, 'c')
    qc = QuantumCircuit(qr, cr)
    qc.h(qr)
    for i in range(p):
        
        for u, v in G.edges:
            qc.cx(qr[u], qr[v])
            qc.rz(2 * gamma[i], qr[v])
            qc.cx(qr[u], qr[v])
        
        
        for qubit in range(n):
            qc.rx(2 * beta[i], qr[qubit])
    
    
    qc.measure(qr, cr)
    
    return qc
import cirq
def swaptest_zaxis(unknown_state=None) : 

    """
            Implement a quantum circuit using the SWAP test algorithm to estimate the angle θ (in radians) between an unknown single-qubit quantum state and the |0⟩ state (Z-axis).
            The input is a single qubit QuantumCircuit unknown_state, and return cirq.Circuit after measurement with key='result'
    """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def swaptest_zaxis(unknown_state: QuantumCircuit) ->QuantumCircuit: 

    qr = QuantumRegister(3, 'q')  
    cr = ClassicalRegister(1, 'c') 
    qc = QuantumCircuit(qr, cr)
    
    qc = qc.compose(unknown_state, [qr[1]])  
    qc.h(qr[0])
    qc.cswap(qr[0], qr[1], qr[2]) 
    qc.h(qr[0])
    qc.measure(qr[0], cr[0])
    return qc
import cirq
def swaptest_individual() : 
    
    """
        Implement a quantum circuit using the SWAP test with individual ancilla qubits for each qubit-to-qubit comparison between two 3-qubit quantum states: |011⟩ and |000⟩.
        Encode |011⟩ on the first set of 3 qubits, and |000⟩ on the second set of 3 qubits. Use 3 ancilla qubits and measure ancilla qubits, return cirq.Circuit after measurement with key='result'.
    """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
def swaptest_individual() ->QuantumCircuit: 
    qr = QuantumRegister(9, 'q')
    cr = ClassicalRegister(3, 'c')
    qc = QuantumCircuit(qr, cr)

    qc.x(qr[1])
    qc.x(qr[2])
    
    qc.h(qr[6])
    qc.h(qr[7])
    qc.h(qr[8])
    qc.cswap(qr[6],qr[0], qr[3])
    qc.cswap(qr[7],qr[1], qr[4])
    qc.cswap(qr[8], qr[2], qr[5])
    qc.h(qr[7])
    qc.h(qr[6])
    qc.h(qr[8])
    qc.measure(qr[6], cr[0])
    qc.measure(qr[7], cr[1])
    qc.measure(qr[8], cr[2])
    return qc
import cirq
from math import pi

def qft_6() : 

    """
        Implement a 6 qubit quantum fourier transform circuit, and return cirq.Circuit after measurement with key='result'.
    """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from math import pi

def qft_6():
    """QFT on 6 qubits"""
    qr = QuantumRegister(6, 'q')
    cr = ClassicalRegister(6, 'c')
    qc = QuantumCircuit(qr,cr)
    for i in range(5, -1, -1):
        qc.h(qr[i])
        for j in range(i):
            qc.cp(pi/2**(i-j), qr[j], qr[i])
    
    for j in range(3): 
        qc.swap(qr[j], qr[5-j])

    qc.measure(qr, cr)
    return qc
import cirq
def qpe_x_gate() : 
    
    """
        Implement a quantum circuit to perform Quantum Phase Estimation using 3 counting qubits and 1 target qubit to estimate the phase of a unitary operator X (Pauli-X gate).
        return cirq.Circuit after measurement with key='result'.
    """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFT

def qpe_x_gate(n_count=3):
    qc = QuantumCircuit(n_count + 1, n_count)
    qc.h(range(n_count))
    qc.x(n_count)

    for qubit in range(n_count):
        repetitions = 2**qubit
        for _ in range(repetitions):
            qc.cx(qubit, n_count) 

    qc.append(QFT(n_count, inverse=True, do_swaps=True).to_gate(), range(n_count))

    qc.measure(range(n_count), range(n_count))

    return qc

print(qpe_x_gate())
import cirq
def Deutsch_Jozsa_Balance_4() : 
    """
        Implement a quantum circuit use DeutschJozsa algorithm to solve a oracle with bitstring "1100", return cirq.Circuit after measurement with key='result'.
    """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
import numpy as np

def Deutsch_Jozsa_Balance_4() -> QuantumCircuit:
    def dj_oracle(case, n):

        oracle_qc = QuantumCircuit(n+1)
        if case == "balanced":
            b = np.random.randint(1,2**n)
            b_str = '1100'
            for qubit in range(len(b_str)):
                if b_str[qubit] == '1':
                    oracle_qc.x(qubit)
            for qubit in range(n):
                oracle_qc.cx(qubit, n)
            for qubit in range(len(b_str)):
                if b_str[qubit] == '1':
                    oracle_qc.x(qubit)
        if case == "constant":
            output = np.random.randint(2)
            if output == 1:
                oracle_qc.x(n)
        
        oracle_gate = oracle_qc.to_gate() 
        return oracle_gate

    def dj_algorithm(oracle, n):
        dj_circuit = QuantumCircuit(n+1, n)
        dj_circuit.x(n)
        dj_circuit.h(n)
        for qubit in range(n):
            dj_circuit.h(qubit)
        dj_circuit.append(oracle, range(n+1))
        for qubit in range(n):
            dj_circuit.h(qubit)
        for i in range(n):
            dj_circuit.measure(i, i)
        return dj_circuit
    
    n = 4  
    oracle_gate = dj_oracle('balanced', n)
    dj_circuit = dj_algorithm(oracle_gate, n)
    
    return dj_circuit
import cirq
def Deutsch_Jozsa_Constant_4() : 
    """
        Implement a quantum circuit use DeutschJozsa algorithm to solve a oracle with bitstring "0000", return cirq.Circuit after measurement with key='result'.
    """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
import numpy as np

def Deutsch_Jozsa_Constant_4() -> QuantumCircuit:
    def dj_oracle(case, n):
        oracle_qc = QuantumCircuit(n+1)
        if case == "balanced":
            b = np.random.randint(1,2**n)
            b_str = '1100'
            for qubit in range(len(b_str)):
                if b_str[qubit] == '1':
                    oracle_qc.x(qubit)
            for qubit in range(n):
                oracle_qc.cx(qubit, n)
            for qubit in range(len(b_str)):
                if b_str[qubit] == '1':
                    oracle_qc.x(qubit)
        if case == "constant":
            output = 0
            if output == 1:
                oracle_qc.x(n)
        oracle_gate = oracle_qc.to_gate() 
        return oracle_gate

    def dj_algorithm(oracle, n):
        dj_circuit = QuantumCircuit(n+1, n)
        dj_circuit.x(n)
        dj_circuit.h(n)
        for qubit in range(n):
            dj_circuit.h(qubit)
        dj_circuit.append(oracle, range(n+1))
        for qubit in range(n):
            dj_circuit.h(qubit)
        for i in range(n):
            dj_circuit.measure(i, i)
        return dj_circuit

    n = 4  
    oracle_gate = dj_oracle('constant', n)
    dj_circuit = dj_algorithm(oracle_gate, n)
    
    return dj_circuit
import cirq
def Simon_11() : 
    """
        Implement a quantum circuit using Simon's algorithm for the case where:
            - The input bit length n = 2
            - The hidden string s = '11'
        return cirq.Circuit after measurement with key='result' qubit about s.
    """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Simon_11() ->QuantumCircuit: 
    qc = QuantumCircuit(4,2)
    qc.h(0)
    qc.h(1)
    for i in range(2):
        qc.cx(i,2)
        qc.cx(i,3)
        qc.h(i)
    qc.measure(2,0)
    qc.measure(3,1)
    
    return qc
import cirq
def Simon_110() : 
    """
        Implement a quantum circuit using Simon's algorithm for the case:
            - The input bit length n = 3
            - The hidden string s = '110'
        return cirq.Circuit after measurement with key='result' of the qubits about s.
    """

Framework: cirq | Category: QUANTUM_ALGO

Solution:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def Simon_110() ->QuantumCircuit: 
    qc = QuantumCircuit(6,3)
    for i in range(3):
        qc.h(i)
    for i in range(3):
        qc.cx(i,i+3)
    qc.cx(1,4)
    qc.cx(1,5)
    for i in range(3):
        qc.h(i)
        qc.measure(i,i)
    return qc