<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 async def get_cred_briefs_by_proof_req_q(self, proof_req_json: str, x_queries_json: str = None) -> str: """ A cred-brief aggregates a cred-info and a non-revocation interval. A cred-brief-dict maps wallet cred-ids to their corresponding cred-briefs. Return json (cred-brief-dict) object mapping wallet credential identifiers to cred-briefs by proof request and WQL queries by proof request referent. Return empty dict on no WQL query and empty requested predicates specification within proof request. Utility util.proof_req2wql_all() builds WQL to retrieve all cred-briefs for (some or all) cred-def-ids in a proof request. For each WQL query on an item referent, indy-sdk takes the WQL and the attribute name and restrictions (e.g., cred def id, schema id, etc.) from its referent. Note that util.proof_req_attr_referents() maps cred defs and attr names to proof req item referents, bridging the gap between attribute names and their corresponding item referents. Raise WalletState if the wallet is closed. :param proof_req_json: proof request as per Verifier.build_proof_req_json(); e.g., :: { "nonce": "1532429687", "name": "proof_req", "version": "0.0", "requested_predicates": {}, "requested_attributes": { "17_name_uuid": { "restrictions": [ { "cred_def_id": "LjgpST2rjsoxYegQDRm7EL:3:CL:17:tag" } ], "name": "name" }, "17_thing_uuid": { "restrictions": [ { "cred_def_id": "LjgpST2rjsoxYegQDRm7EL:3:CL:17:tag" } ], "name": "thing" } } } :param x_queries_json: json list of extra queries to apply to proof request attribute and predicate referents; e.g., :: { "17_thing_uuid": { # require attr presence on name 'thing', cred def id from proof req above "$or": [ { "attr::name::value": "J.R. 'Bob' Dobbs" }, { "attr::thing::value": "slack" }, ] }, } :return: json (cred-brief-dict) object mapping wallet cred ids to cred briefs; e.g., :: { "b42ce5bc-b690-43cd-9493-6fe86ad25e85": { "interval": null, "cred_info": { "schema_id": "LjgpST2rjsoxYegQDRm7EL:2:non-revo:1.0", "rev_reg_id": null, "attrs": { "name": "J.R. \"Bob\" Dobbs", "thing": "slack" }, "cred_rev_id": null, "referent": "b42ce5bc-b690-43cd-9493-6fe86ad25e85", "cred_def_id": "LjgpST2rjsoxYegQDRm7EL:3:CL:17:tag" } }, "d773434a-0080-4e3e-a03b-f2033eae7d75": { "interval": null, "cred_info": { "schema_id": "LjgpST2rjsoxYegQDRm7EL:2:non-revo:1.0", "rev_reg_id": null, "attrs": { "name": "Chicken Hawk", "thing": "chicken" }, "cred_rev_id": null, "referent": "d773434a-0080-4e3e-a03b-f2033eae7d75", "cred_def_id": "LjgpST2rjsoxYegQDRm7EL:3:CL:17:tag" } } } """


        LOGGER.debug(
            ('HolderProver.get_cred_briefs_by_proof_req_q >>> proof_req_json: %s, x_queries_json: %s'),
            proof_req_json,
            x_queries_json)

        if not self.wallet.handle:
            LOGGER.debug('HolderProver.get_cred_briefs_by_proof_req_q <!< Wallet %s is closed', self.name)
            raise WalletState('Wallet {} is closed'.format(self.name))

        def _pred_filter(brief):
            nonlocal pred_refts
            for attr, preds in pred_refts.get(brief['cred_info']['cred_def_id'], {}).items():
                if any(Predicate.get(p[0]).value.no(brief['cred_info']['attrs'][attr], p[1]) for p in preds.values()):
                    return False
            return True

        rv = {}
        item_refts = set()
        x_queries = json.loads(x_queries_json or '{}')
        for k in x_queries:
            x_queries[k] = canon_cred_wql(x_queries[k])  # indy-sdk requires attr name canonicalization
            item_refts.add(k)

        proof_req = json.loads(proof_req_json)
        item_refts.update(uuid for uuid in proof_req['requested_predicates'])
        if not x_queries:
            item_refts.update(uuid for uuid in proof_req['requested_attributes'])  # get all req attrs if no extra wql
        handle = await anoncreds.prover_search_credentials_for_proof_req(
            self.wallet.handle,
            proof_req_json,
            json.dumps(x_queries) if x_queries else None)
        pred_refts = proof_req_pred_referents(proof_req)

        try:
            for item_referent in item_refts:
                count = Wallet.DEFAULT_CHUNK
                while count == Wallet.DEFAULT_CHUNK:
                    fetched = json.loads(await anoncreds.prover_fetch_credentials_for_proof_req(
                        handle,
                        item_referent,
                        Wallet.DEFAULT_CHUNK))
                    count = len(fetched)
                    for brief in fetched:  # apply predicates from proof req here
                        if brief['cred_info']['referent'] not in rv and _pred_filter(brief):
                            rv[brief['cred_info']['referent']] = brief
        finally:
            await anoncreds.prover_close_credentials_search_for_proof_req(handle)

        rv_json = json.dumps(rv)
        LOGGER.debug('HolderProver.get_cred_briefs_by_proof_req_q <<< %s', rv_json)
        return rv_json